我目前有一个媒体播放器,我正在尝试从源路径获取重定向地址。由于媒体播放器不支持重定向处理,我试图通过创建httpurlconnection等来获取重定向的url路径。但是,我不确定我是否正确行事。任何帮助,将不胜感激。感谢。
代码:
Log.d(TAG, "create url - test");
URL testUrl = new URL(path);
HttpURLConnection conn = (HttpURLConnection)testUrl.openConnection();
String test = conn.getURL().toString();
String test1 = conn.getHeaderField(2);
String test2 = conn.toString();
Log.d(TAG, "normal stuff test is: " + test);
Log.d(TAG, "header field test is: " + test1);
Log.d(TAG, "url to string is: " + test2);
答案 0 :(得分:0)
以下代码遵循URL重定向的一跳。通过使用HTTP HEAD请求而不是GET,消耗掉的带宽。扩展此方法以处理多个跃点应该是相当直接的。
public URI followRedirects(URI original) throws ClientProtocolException, IOException, URISyntaxException
{
HttpHead headRequest = new HttpHead(original);
HttpResponse response = client.execute(headRequest);
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY ||
statusCode == HttpStatus.SC_MOVED_TEMPORARILY)
{
String location = response.getHeaders("Location")[0].toString();
String redirecturl = location.replace("Location: ", "");
return new URI(redirecturl);
}
return original;
}
它假设您已经在client
字段中设置了HttpClient。