2010-11-23 10 views
2

静的なHTTPS接続メソッドで大きな問題が発生しています。 毎秒のリクエストが失敗すると、HttpsUrlConnection.getResponseCode()-1を返します。したがって、2回ごとの呼び出しがうまく動作し、期待どおりのデータが返されます。静的メソッドの2番目ごとのHttpsUrlConnection要求がAndroidで失敗する

私のアプリケーションのさまざまなコーナーで使用している静的クラスのメソッドです。メソッドが最初に返ってきたときに、私が正しくクリーンアップしないものがあり、問題の原因が何であれ、メソッドの2回目の呼び出しで破壊されるかもしれないと思います。しかし、私は手がかりを見つけるのに苦労しています。

私は現在、このクラスを使用して、無効なSSL証明書を持つホストと通信しています。アプリの最終版でこれを使用するつもりはないが、今はお金を節約する必要がある。 ;)

public static String makeInvalidHTTPSRequest(String url, String[] postVars, String userName, String userPass, Context ctx) throws MalformedURLException, IOException, NoSuchAlgorithmException, KeyManagementException { 
    StringBuffer sb = new StringBuffer(); 
    String serverAuth = null; 
    String serverAuthBase64 = null; 
    StringBuffer urlParameters = new StringBuffer(); 
    InputStream rcvdInputStream = null; 

    if (checkNetworkAvailability(ctx) == false) { 
     GeneralMethods.writeLog("Network unavailable", 1, GeneralMethods.class); 
     return null; 
    } 

    SSLContext sc = null; 
    sc = SSLContext.getInstance("TLS"); 
    sc.init(null, new TrustManager[] { new KTPTrustManager() }, new SecureRandom()); 

    GeneralMethods.writeLog("makeInvalidHTTPSRequest-> " + url + ", " + userName + ", " + userPass, 0, GeneralMethods.class); 

    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); 
    HttpsURLConnection.setDefaultHostnameVerifier(new KTPHostnameVerifier()); 
    HttpsURLConnection con = null; 
    con = (HttpsURLConnection) new URL(url).openConnection(); 

    if (userName != null) { 
     serverAuth = userName + ":" + userPass; 
     serverAuthBase64 = KTPBase64.encode(serverAuth.getBytes()); 
    } 

    try { 
     String[] tmpPair = null; 
     con.setRequestMethod("POST"); 

     if (serverAuthBase64 != null) 
      con.setRequestProperty("Authorization", "Basic " + serverAuthBase64); 

     con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
     if (postVars != null) { 
      for (int i = 0; i < postVars.length; i++) { 
       tmpPair = postVars[i].toString().split("="); 

       if (i > 0) 
        urlParameters.append("&" + tmpPair[0] + "=" + URLEncoder.encode(tmpPair[1], "UTF-8")); 
       else 
        urlParameters.append(tmpPair[0] + "=" + URLEncoder.encode(tmpPair[1], "UTF-8")); 
      } 
      con.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.toString().getBytes().length)); 
     } 

     con.setUseCaches(false); 
     con.setDoOutput(true); 
     con.setDoInput(true); 

     DataOutputStream wr = new DataOutputStream (con.getOutputStream()); 

     if (postVars != null) 
      wr.writeBytes (urlParameters.toString()); 

     wr.flush(); 
     wr.close(); 

     if (con.getResponseCode() == 200) { 
      globalRetries = 0; 
      rcvdInputStream = con.getInputStream(); 
     } 
     else if (con.getResponseCode() == 401) { 
      con.disconnect(); 
      GeneralMethods.writeLog("error 401", 2, GeneralMethods.class); 
      con = null; 
      // SEND CONNECTION PROBLEM-INTENT 
      return null; 
     } 
     else { 
      GeneralMethods.writeLog("error - connection response code " + con.getResponseCode() + ": " + con.getResponseMessage() + " (length: "+ con.getContentLength() +")\n\n", 1, GeneralMethods.class); 
      con.disconnect(); 
      con = null; 
      // SEND CONNECTION PROBLEM-INTENT 
      return null; 
     } 

     BufferedReader br = new BufferedReader(new InputStreamReader(rcvdInputStream), 8192); 
     String line; 
     while ((line = br.readLine()) != null) { 
       sb.append(line); 
     } 
     con.disconnect(); 
     con = null; 
    } 
    catch(Exception e) { 
     handleException(e, 2, GeneralMethods.class); 
    } 

    GeneralMethods.writeLog("makeInvalidHTTPSRequest: Response is \"" + sb.toString() + "\"\n\n", 0, GeneralMethods.class); 

    if(con != null) { 
     con.disconnect(); 
     con = null; 
    } 
    if (sb.toString().trim() == "") 
     return null; 
    else 
     return sb.toString(); 
} 

ありがとうございました!

敬具
S.

答えて

関連する問題