Android DefaultHttpClinet 重定向
2015-12-03 11:00
711 查看
一:实现重定向的代码
HttpParams httpParams = new BasicHttpParams();
// 设置连接超时和 Socket 超时,以及 Socket 缓存大小
HttpConnectionParams.setConnectionTimeout(httpParams, 8000);
HttpConnectionParams.setSoTimeout(httpParams, 8000);
HttpConnectionParams.setSocketBufferSize(httpParams, 8 * 1024);
// 设置重定向,缺省为 true
HttpClientParams.setRedirecting(httpParams, true);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);
HttpGet request = new HttpGet(url);
httpClient.setRedirectHandler(new RedirectHandler() {
@Override
public boolean isRedirectRequested(HttpResponse response,HttpContext context) {
Header[] header = response.getHeaders("Location");
if (header.length > 0) {
mRedirectUrl = header[0].getValue();
return true;
}
return false;
}
@Override
public URI getLocationURI(HttpResponse response, HttpContext context)
throws ProtocolException {
try {
return new URI(response.getLastHeader("Location").getValue());
} catch (URISyntaxException e) {
Log.d("VideoPlay", "Redirect failed : ");
e.printStackTrace();
}
return null;
}
});
final HttpResponse response =
httpClient.execute(request);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
}
二:使用curl -v url命令查看重定向的内容。如下示例:
curl -v "http://122.96.53.143/88888888/16/20150901/269050627/index.m3u8?rrsip=122.96.53.143&servicetype=0&icpid=&accounttype=1&limitflux=-1&limitdur=-1&accountinfo=:20151204104427,otttest201509180181,218.241.193.75,20151204104427,yp_163715,F0CFD894028F28B0F2541CEDC7FD6CF8,0,1,0,,1,1,2634000600,26340003,879847,1,END"
* Hostname was NOT found in DNS cache
* Trying 122.96.53.143...
* Connected to 122.96.53.143 (122.96.53.143) port 80 (#0)
> GET /88888888/16/20150901/269050627/index.m3u8?rrsip=122.96.53.143&servicetype=0&icpid=&accounttype=1&limitflux=-1&limitdur=-1&accountinfo=:20151204104427,otttest201509180181,218.241.193.75,20151204104427,yp_163715,F0CFD894028F28B0F2541CEDC7FD6CF8,0,1,0,,1,1,2634000600,26340003,879847,1,END
HTTP/1.1
> User-Agent: curl/7.35.0
> Host: 122.96.53.143
> Accept: */*
>
< HTTP/1.1 302 Moved Temporarily
< Location: http://122.96.53.85:80/88888888/16/20150901/269050627/index.m3u8?rrsip=122.96.53.143&servicetype=0&icpid=&accounttype=1&limitflux=-1&limitdur=-1&accountinfo=:20151204104427,otttest201509180181,218.241.193.75,20151204104427,yp_163715,F0CFD894028F28B0F2541CEDC7FD6CF8,0,1,0,,1,1,2634000600,26340003,879847,1,END&icpid=88888888&from=1
* no chunk, no close, no size. Assume close to signal end
<
* Closing connection 0
1.Location:内容就是重定向的内容。
2.HTTP/1.1 302 Moved Temporarily:表明发生了302重定向,302重定向是暂时重定向。
A line starting with '>' means "header data" sent by curl, '<' means "header data" received by curl that is hidden in
normal cases, and a line starting with '*' means additional info provided by curl.
HttpParams httpParams = new BasicHttpParams();
// 设置连接超时和 Socket 超时,以及 Socket 缓存大小
HttpConnectionParams.setConnectionTimeout(httpParams, 8000);
HttpConnectionParams.setSoTimeout(httpParams, 8000);
HttpConnectionParams.setSocketBufferSize(httpParams, 8 * 1024);
// 设置重定向,缺省为 true
HttpClientParams.setRedirecting(httpParams, true);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);
HttpGet request = new HttpGet(url);
httpClient.setRedirectHandler(new RedirectHandler() {
@Override
public boolean isRedirectRequested(HttpResponse response,HttpContext context) {
Header[] header = response.getHeaders("Location");
if (header.length > 0) {
mRedirectUrl = header[0].getValue();
return true;
}
return false;
}
@Override
public URI getLocationURI(HttpResponse response, HttpContext context)
throws ProtocolException {
try {
return new URI(response.getLastHeader("Location").getValue());
} catch (URISyntaxException e) {
Log.d("VideoPlay", "Redirect failed : ");
e.printStackTrace();
}
return null;
}
});
final HttpResponse response =
httpClient.execute(request);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
}
二:使用curl -v url命令查看重定向的内容。如下示例:
curl -v "http://122.96.53.143/88888888/16/20150901/269050627/index.m3u8?rrsip=122.96.53.143&servicetype=0&icpid=&accounttype=1&limitflux=-1&limitdur=-1&accountinfo=:20151204104427,otttest201509180181,218.241.193.75,20151204104427,yp_163715,F0CFD894028F28B0F2541CEDC7FD6CF8,0,1,0,,1,1,2634000600,26340003,879847,1,END"
* Hostname was NOT found in DNS cache
* Trying 122.96.53.143...
* Connected to 122.96.53.143 (122.96.53.143) port 80 (#0)
> GET /88888888/16/20150901/269050627/index.m3u8?rrsip=122.96.53.143&servicetype=0&icpid=&accounttype=1&limitflux=-1&limitdur=-1&accountinfo=:20151204104427,otttest201509180181,218.241.193.75,20151204104427,yp_163715,F0CFD894028F28B0F2541CEDC7FD6CF8,0,1,0,,1,1,2634000600,26340003,879847,1,END
HTTP/1.1
> User-Agent: curl/7.35.0
> Host: 122.96.53.143
> Accept: */*
>
< HTTP/1.1 302 Moved Temporarily
< Location: http://122.96.53.85:80/88888888/16/20150901/269050627/index.m3u8?rrsip=122.96.53.143&servicetype=0&icpid=&accounttype=1&limitflux=-1&limitdur=-1&accountinfo=:20151204104427,otttest201509180181,218.241.193.75,20151204104427,yp_163715,F0CFD894028F28B0F2541CEDC7FD6CF8,0,1,0,,1,1,2634000600,26340003,879847,1,END&icpid=88888888&from=1
* no chunk, no close, no size. Assume close to signal end
<
* Closing connection 0
1.Location:内容就是重定向的内容。
2.HTTP/1.1 302 Moved Temporarily:表明发生了302重定向,302重定向是暂时重定向。
A line starting with '>' means "header data" sent by curl, '<' means "header data" received by curl that is hidden in
normal cases, and a line starting with '*' means additional info provided by curl.
相关文章推荐
- ajax的基础:XMLHttpRequest对象
- java必学必会之网络编程
- 【python】网络编程-SocketServer 实现客户端与服务器间非阻塞通信
- MapServer 之 发布网络要素服务(WFS-Web Feature Service)
- 网络编程
- Android6.0找不到org.apache.http.Header的问题
- TCP/IP模型各个层次的功能和协议
- 【python】网络编程-UDP协议套接字
- IOS开发中如何检测网络连接
- 10021---Java+MySQL实现网络爬虫程序
- JAVA 进行https传输中文乱码解决
- [网络管理]全双工与半双工的区别
- 32、OSPF在帧中继中不同网络类型配置总结
- xCODE7.1进行网络访问的设置
- iOS开发-网络篇 文件的上传
- Android Volley完全解析(二),使用Volley加载网络图片
- 程序中使用ajax时,type为put,或者delete时在 IIS上没效果,发生HTTP Error 405.0 - Method Not Allowed
- "javax.servlet.http.HttpServlet"was not found on the java build path
- TCP,IP详解,卷一:协议学习笔记之第一章概述
- 介绍一篇关于session的好文章