今天学习HttpClient发现一个问题,无法解决。
我用HttpClient去连接一个网站A,手机开着wifi连接着一个学校的校园无线网,但由于没有登录,所以是无法通过校园无线网上网的,此时httpclient是无法连接到网站A的,但是httpclient却不会抛出IOException。如果关闭手机wifi和移动网络,则会正常抛出IOException。哪位大神指点迷津。
代码如下:
try {
DefaultHttpClient httpclient=new DefaultHttpClient();
HttpGet get1=new HttpGet(“http://202.116.0.176/”);
HttpResponse re1 = httpclient.execute(get1);
CookieStore cooStore= httpclient.getCookieStore(); //获取cookie
List<Cookie> lists=cooStore.getCookies();
for(Cookie list:lists){
cookiename=list.getName();
cookievalue=list.getValue();
Log.d(“cookiename”, cookiename); //输出cookiename
Log.d(“cookievalue”, cookievalue); //输出cookievalue
}
HttpEntity entity1=re1.getEntity();
String html=EntityUtils.toString(entity1);
doc=Jsoup.parse(html);
Elements hiddeninput1=doc.select(“input[name=__VIEWSTATE]”);
__VIEWSTATE=hiddeninput1.attr(“value”);
Elements hiddeninput2=doc.select(“input[name=__VIEWSTATEGENERATOR]”);
__VIEWSTATEGENERATOR=hiddeninput2.attr(“value”);
Elements hiddeninput3=doc.select(“input[name=__EVENTVALIDATION]”);
__EVENTVALIDATION=hiddeninput3.attr(“value”);
//输出隐藏输入的值,调试用
Log.d(“__VIEWSTATE”,__VIEWSTATE );
Log.d(“__VIEWSTATEGENERATOR”, __VIEWSTATEGENERATOR);
Log.d(“__EVENTVALIDATION”, __EVENTVALIDATION);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
handler.sendEmptyMessage(0); //异常处理
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
handler.sendEmptyMessage(0); //异常处理
}