开发者社区> 问答> 正文

android 使用 模拟浏览器,无法得到返回值

代码如下,根据输入的url地址来返回状态码。
可是,httpconnection.getResponseCode();返回的都是200,没有实际返回访问值。
请高手指点。

public String stat_is(String strurl)
 {
     HttpURLConnection  httpconnection;
     int rcode=0;
    String respon = "ok";
    httpconnection = null;
    //String strurl=siteaddr;


    try{

        URL url = new URL(strurl);
        httpconnection = (HttpURLConnection) url.openConnection(); 

        httpconnection.setConnectTimeout(2*1000);
        httpconnection.setRequestMethod("GET");
        httpconnection.setDoInput(true);    

        httpconnection.setRequestProperty("Charset", "UTF-8");
        httpconnection.setUseCaches(false); 
        httpconnection.setRequestProperty("Connection", "Keep-Alive");  
        //httpconnection.setRequestProperty("Content-type", "application/x-java-serialized-object");   
        //InputStream input = httpconnection.getInputStream();
        //readStream(input);
        //httpconnection.setRequestProperty("Content-Type", "application/xhtml+xml");
        //mes = httpconnection.getResponseMessage();
        httpconnection.connect();
        rcode = httpconnection.getResponseCode();
        if (rcode == 200){
            respon="ok";
        }
        else 
        {
            respon="error";
        }


    }
    catch (Exception e) {  
           e.printStackTrace();  

               }
    finally {   
              if (httpconnection != null) {   
                    httpconnection.disconnect();   
             }   

    }


    return respon;  

}

展开
收起
爵霸 2016-06-20 10:35:40 2001 0
0 条回答
写回答
取消 提交回答
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载