302 使用 Dropbox 短超链接在 Android 中从 http 重定向到 https
Posted
技术标签:
【中文标题】302 使用 Dropbox 短超链接在 Android 中从 http 重定向到 https【英文标题】:302 Redirect from http to https in Android using Dropbox short Hyperlinks 【发布时间】:2013-02-07 10:51:04 【问题描述】:我有一个应用程序,它在正常情况下从云存储加载和播放音乐没有问题,除了 Dropbox 短链接。 这些链接使用 302 标头重定向到 https 链接:-
302 Found 资源位于 https://www.dropbox.com/s/jhzh3woy3qxblck/05%20-%20Cinema.ogg;你 应该会自动重定向。
此代码的结构是双重静态方法,首先[按时间顺序]寻找重定向,然后将数据获取到文件。
我正在尝试让它工作,因为第二个链接目前让我从 Dropbox 下载了很多不相关的 HTML,而不是所需的文件本身!
/**
* Return an Audio File from a URL String
* throws IOException
*
* @param url the URL which provides the target
* @return File - audio file
*/
public static File getAudioFile(String urlString, File f)
String newUrl = null;
try
newUrl = getRedirect(urlString);
catch (ClientProtocolException e)
Log.e(TAG, "ClientProtocolException Error getting Redirect ", e);
catch (IOException e)
Log.e(TAG, "IOException Error getting Redirect", e);
if (newUrl != null)
urlString = newUrl;
else
Log.i(TAG, "IOException Error getting Redirect because its null");
try
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setInstanceFollowRedirects(true);
conn.setReadTimeout(40 * 1000);
conn.setConnectTimeout(45 * 1000);
conn.setRequestMethod("GET");
conn.setRequestProperty("Connection", "close");
conn.setDoInput(true);
conn.setDefaultUseCaches(true);
// Starts the input from the URL
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
BufferedOutputStream bof = new BufferedOutputStream(new FileOutputStream(f));
// write the inputStream to the FileOutputStream
byte [] bytes = new byte [409600];
int block;
while((block = bis.read(bytes, 0, bytes.length)) > 0)
bof.write(bytes, 0, block);
Log.d(TAG, "Write a block of " + block);
bof.flush();
bof.close();
bis.close();
is.close();
conn.disconnect();
catch (Exception e)
Log.e(TAG, "Error getting Audio File", e);
return f;
/**
* Get the new url from a http redirect
* throws IOException
*
* @param url the URL which provides the target
* @return url - the single url redirect
*/
public static String getRedirect(String urlString) throws ClientProtocolException, IOException
HttpParams httpParameters = new BasicHttpParams();
HttpClientParams.setRedirecting(httpParameters, false);
HttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpGet httpget = new HttpGet(urlString);
HttpContext context = new BasicHttpContext();
HttpResponse response = httpClient.execute(httpget, context);
// If we didn't get a '302 Found' we aren't being redirected.
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_MOVED_TEMPORARILY)
throw new IOException(response.getStatusLine().toString());
Header loc[] = response.getHeaders("Location");
return loc.length > 0 ? loc[loc.length -1].getValue() : null;
【问题讨论】:
我暂时搁置了这个问题。最终,似乎一些短超链接 [在我的情况下为 Dropbox 音频] 实际上重定向到 html 下载屏幕。恕我直言,这对于链接应该做的事情是没有用的。 【参考方案1】:我遇到了同样的问题。 我发现我可以从 urlConnection.getHeaderField("Location"); 获取新的 URL 请参考以下代码。
url = new URL(urlString);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
String ResponseCode = urlConnection.getResponseCode();
String ContentType = urlConnection.getContentType();
if ( result.ResponseCode == HttpURLConnection.HTTP_MOVED_TEMP || result.ResponseCode == HttpURLConnection.HTTP_MOVED_PERM )
String Location = urlConnection.getHeaderField("Location");
问候, 杰克
【讨论】:
以上是关于302 使用 Dropbox 短超链接在 Android 中从 http 重定向到 https的主要内容,如果未能解决你的问题,请参考以下文章