2012-09-05 77 views
0

我正在測試Android設備上的HTTPS後,我有2臺Android設備,此代碼工作完美我的2.3設備上,但將被擋在我的4.1設備「RETCODE = urlCon.getResponseCode();」 ,我也檢查過嗅探器中的數據包,發送了請求並且服務器確實回覆了https響應,但它仍然被阻止,直到超時。HTTPS POST在Android 4.0的

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; 

} 

回答

0

我想你有清單內的Internet權限?我想這個問題是在「用戶代理」內部調用setRequestProperty的一個或者也許在一個又一個,但不是在代碼的其餘部分。

+0

感謝您的回覆是的,權限已被應用,但我只是想知道爲什麼它適用於2.3設備 – mailart

0

嘗試把

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

地方在你的代碼。