0

我正在测试 android 上的 https 帖子,我有 2 个 android 设备,此代码在我的 2.3 设备上完美运行,但将在我的 4.1 设备中被阻止在“retcode=urlCon.getResponseCode();”,我也检查了数据包嗅探 post 请求已发送并且服务器确实回复了 https 响应,但它仍然被阻止直到超时。

private String GetHttpPage(HttpsURLConnection urlsCon,String host,String url,String reference,boolean type,String curcookie,String postparam){
 String query = postparam; 
     byte[] entitydata = query.getBytes();
     String line="";
     HttpURLConnection urlCon=urlsCon;
     try{
         String domain="";
         String fullurl=host+url;
         if(urlCon==null){
             if(host.indexOf("https")!=-1){
                 urlCon =GetHttpsConnect(fullurl);
                    domain=host.substring(8);    
            }
            else{
                 urlCon =GetHttpConnect(fullurl);
                domain=host.substring(7);
            }
         }
         urlCon.setConnectTimeout(30000);  
         urlCon.setReadTimeout(30000); 


          if(reference!=null)
              urlCon.setRequestProperty("Referer", reference);

          urlCon.setRequestProperty("Cache-Control", "no-cache"); 
          urlCon.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)");  
          urlCon.setRequestProperty("Accept-Encoding", "identity");  
        urlCon.setRequestProperty("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*");  
        urlCon.setRequestProperty("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");  
         urlCon.setRequestProperty("connection", "Keep-Alive");  
         urlCon.setRequestProperty("Host", domain);  
   //      

         if(curcookie.length()>0)
             urlCon.setRequestProperty("Cookie", curcookie);

         if(type){
             urlCon.setDoOutput(true);
             urlCon.setDoInput(true);
             urlCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
             urlCon.setRequestProperty("Content-Length",String.valueOf(entitydata.length));
             urlCon.setRequestMethod("POST");
         }
         else{


            urlCon.setDoOutput(false);
            urlCon.setDoInput(true);
            urlCon.setRequestMethod("GET");

         }

         urlCon.connect();

         if(type){

             OutputStream outStream = urlCon.getOutputStream();
             outStream.write(entitydata, 0, entitydata.length);
       //      outStream.write(entitydata);
             outStream.flush();
             outStream.close();
         }


         retcode=urlCon.getResponseCode();
         if(urlCon.getHeaderField("Location")!=null)
             redirect=urlCon.getHeaderField("Location");

         Map   m=urlCon.getHeaderFields();
         Set   set=m.entrySet();
         Iterator   it=set.iterator();
         while(it.hasNext())
         {
           Map.Entry   me=(Map.Entry)it.next();
           String skey=me.getKey()!=null?me.getKey().toString():"";
           String svalue=me.getValue()!=null?me.getValue().toString():"";
           if(skey.compareToIgnoreCase("Set-Cookie")==0){
               String tempcookie=svalue;
               tempcookie=tempcookie.substring(1, tempcookie.length()-1);
               cookie+=ProcessCookie(tempcookie);
               //cookie=cookie.substring(0, cookie.length()-2);
           }
         } 


 //        if(urlCon.getHeaderField("Set-Cookie")!=null)
   //        cookie=urlCon.getHeaderField("Set-Cookie");


         BufferedReader in = new BufferedReader(new InputStreamReader(urlCon.getInputStream()));

         String subline="";
         while ((subline = in.readLine()) != null) {

            line+=subline;
         }
         in.close();

     }
     catch(IOException e){
         String aaa=e.toString();
         Log.e("xx", aaa);
     }
     catch(Exception e){
         String aaa=e.toString();
         Log.e("xx", aaa);
     }

     if(urlCon!=null)
         urlCon.disconnect();
     return line;

 }
4

2 回答 2

0

试着放

System.setProperty("http.keepAlive", "false");

在您的代码中的某处。

于 2013-01-15T13:09:49.967 回答
0

我想您在清单中拥有 Internet 权限?我认为问题出在 setRequestProperty 之一或另一个中的“用户代理”中,但不在其余代码中。

于 2012-09-05T15:53:59.427 回答