Android DefaultHttpClinet 重定向

一:实现重定向的代码

       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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值