https在android 4.0中发布

时间:2012-09-05 15:25:07

标签: android https httpurlconnection

我正在测试一个关于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;

 }

2 个答案:

答案 0 :(得分:0)

我认为你在清单中有Internet权限吗?我认为问题出在setRequestProperty中的一个“User-Agent”中,或者在另一个中,但在其余的代码中没有。

答案 1 :(得分:0)

尝试放

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

代码中的某处。