302重定向从http使用Dropbox的短期超链接在Android的HTTPS [英] 302 Redirect from http to https in Android using Dropbox short Hyperlinks
问题描述
我有一个应用程序,它加载并播放来自云存储的音乐没有问题,在正常情况下,当它涉及到的的Dropbox 短链接除外。
这些链接使用302头重定向到HTTPS链接: -
302找到资源被发现
https://www.dropbox.com/s/jhzh3woy3qxblck/05% 20-%20Cinema.ogg ;您
应被自动重定向。
块引用>这code的结构是双静态方法,先[时间顺序]寻找一个重定向和第二的数据到一个文件中。
我试图得到它的工作,因为第二个链接目前让我从Dropbox的下载了很多不相关的HTML,而不是所需要的文件本身!
/ **
*从URL字符串返回音频文件
*抛出IOException异常
*
* @参数url可提供目标网址
* @返回文件 - 音频文件
* /
公共静态文件getAudioFile(字符串urlString,文件f){
字符串NEWURL = NULL;
尝试{
NEWURL = getRedirect(urlString);
}赶上(ClientProtocolException E){
Log.e(TAGClientProtocolException错误获取重定向,E);
}赶上(IOException异常五){
Log.e(TAG,IOException异常错误获取重定向,E);
}
如果(NEWURL!= NULL){
urlString = NEWURL;
}
其他{
Log.i(TAG,IOException异常错误获取重定向,因为它的空);
} 尝试{
网址URL =新的URL(urlString);
HttpURLConnection的康恩=(HttpURLConnection类)url.openConnection();
conn.setInstanceFollowRedirects(真);
conn.setReadTimeout(40 * 1000);
conn.setConnectTimeout(45 * 1000);
conn.setRequestMethod(GET);
conn.setRequestProperty(连接,关闭);
conn.setDoInput(真);
conn.setDefaultUseCaches(真);
//从URL启动输入
conn.connect();
InputStream为= conn.getInputStream();
二BufferedInputStream为=新的BufferedInputStream(是);
BOF的BufferedOutputStream =新的BufferedOutputStream(新的FileOutputStream(f)条); //写的InputStream到的FileOutputStream
字节[]字节=新的字节[409600]。
INT块;
而((块= bis.read(字节,0,bytes.length))大于0){
bof.write(字节,0,块);
Log.d(TAG,写块+块);
} bof.flush();
bof.close();
bis.close();
is.close();
conn.disconnect();
}赶上(例外五){
Log.e(TAG错误获取音频文件,E);
}
返回F;
}
/ **
*从HTTP重定向获取新的URL
*抛出IOException异常
*
* @参数url可提供目标网址
* @返回URL - 单一的URL重定向
* /
公共静态字符串getRedirect(字符串urlString)抛出ClientProtocolException,IOException异常{
的HttpParams httpParameters =新BasicHttpParams();
HttpClientParams.setRedirecting(httpParameters,FALSE); HttpClient的HttpClient的=新DefaultHttpClient(httpParameters);
HTTPGET HTTPGET =新HTTPGET(urlString);
HttpContext的背景下=新BasicHttpContext(); HTT presponse响应= httpClient.execute(HTTPGET,背景); //如果我们没有得到一个发现302我们不是被重定向。
如果(response.getStatusLine()的getStatus code()!= HttpStatus.SC_MOVED_TEMPORARILY)
抛出新IOException异常(response.getStatusLine()的toString()); 头LOC [] = response.getHeaders(位置);
返回loc.length> 0? LOC [loc.length -1] .getValue():空;
}
解决方案我遇到了同样的问题。
我发现我可以从 urlConnection.getHeaderField(位置)获得新的URL; 的
请参考以下code。{
URL =新的URL(urlString);
URLConnection的=(HttpURLConnection类)url.openConnection();
urlConnection.connect();
字符串响应code = urlConnection.getResponse code();
字符串的ContentType = urlConnection.getContentType();
如果(result.Response code == || HttpURLConnection.HTTP_MOVED_TEMP result.Response code == HttpURLConnection.HTTP_MOVED_PERM)
{
字符串位置= urlConnection.getHeaderField(位置);
}
}问候,
杰克I have an app which loads and plays music from cloud storage without problems in normal circumstances, except when it comes to Dropbox short links. These links use a 302 header to redirect to an https link:-
302 Found The resource was found at https://www.dropbox.com/s/jhzh3woy3qxblck/05%20-%20Cinema.ogg; you should be redirected automatically.
The structure of this code is a double static method, first [chronologically] to look for a redirect and second to get the data to a file.
I'm trying to get it to work because the second link currently makes me download a lot of irrelevant HTML from Dropbox, rather than the required file itself!
/** * 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; }
解决方案I met the same problem. I found that I can get new URL from urlConnection.getHeaderField("Location"); Please refer to below code.
{ 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"); } }
Regards, Jack
这篇关于302重定向从http使用Dropbox的短期超链接在Android的HTTPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!