您的位置:首页 > 理论基础 > 计算机网络

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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: