当前位置:操作系统 > 安卓/Android >>

android httpConnection获取301/302请求的内容

最后在通过HttpURLConnection获取网页内容时发现一个小bug.发出请求时一直返回400  Bad Request,获取流数据时返回-1,后面查找发现是服务端进行跳转了。
  解决办法很简单,设置setInstanceFollowRedirects为false就行,注明不进行跳转,也可以通过getHeaderField("Location")获取到要跳转的URL。
       看看SDK的说明,编码之后熟读一下相关的API还是好处多,不然绕了半天才找到方法。
public void setInstanceFollowRedirects (boolean followRedirects)

Since: API Level 1
Sets whether this connection follows redirects.

Parameters

followRedirects 
true if this connection will follows redirects, false otherwise.
 
 
本文出自 “迷途小书童” 博客

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,