2013-09-03 2 views
2

웹 서비스를 사용하는 안드로이드 용 응용 프로그램에서 작업하는 동안 잘못된 요청 (응답 코드 400) android 버전 4.0.3 및 4.3에서 일부 데이터를 검색 할 때 오류 메시지가 표시됩니다. 그러나 똑같은 코드를 사용하여 동일한 요청을 보낼 때 Android 2.3.3 버전을 사용하는 장치에서는 문제없이 작동합니다. 또한 HttpsURLConnection 대신 httpGet을 사용하여 시도했지만, 모든 버전의이 기능은 추가 보안이 필요하므로 솔루션을 제공하지 않습니다.웹 서비스 요청은 안드로이드 (2.3.3)의 이전 버전에서 작동하지만 이후 버전 (4.0.3, 4.3)에서는 작동하지 않습니다.

private String executeRequest(String urlAddress) 
{ 
    String responce = null; 
    String msg = null; 
    int error = 0; 
    try { 
     URL url = new URL(urlAddress); 
     HttpsURLConnection connection = (HttpsURLConnection)url.openConnection(); 
     SSLSocketFactory factory = SecureSocketFactory.getSSLSocketFactory(); 
     connection.setSSLSocketFactory(factory); 

     connection.setHostnameVerifier(new Verifier()); 
     connection.setDoOutput(true); 
     connection.setDoInput(true); 

     if (method == RequestMethod.POST) 
     { 
      connection.setRequestMethod("POST"); 
     } 
     msg = connection.getResponseMessage(); 
     error = connection.getResponseCode(); 
     if ("OK".equals(msg)) 
     { 
      InputStream content = (InputStream) connection.getContent(); 
      responce = convertStreamToString(content); 
     } 
     else 
     { 
      responce = "Error " + error; 
     } 
     connection.disconnect(); 

    } catch (Exception e) { 
     responce = e.toString(); 
    } 

    return responce; 
} 

그리고 SecureSocketFactory.getSSLSocketFactory()의 코드를 : 다음과 같이

내 코드는

public static SSLSocketFactory getSSLSocketFactory() 
    throws IOException 
{ 
    if(ssf_ == null) 
    { 
     javax.net.ssl.KeyManager kms[] = null; 
     javax.net.ssl.TrustManager tms[] = null; 
     SSLContext context = null; 
     try 
     { 
      tms = CustomTrustManager.getTrustManagers(); 
      context = SSLContext.getInstance("TLS"); 
      context.init(kms, tms, null); 
     } 
     catch(GeneralSecurityException e) 
     { 
      IOException io = new IOException(e.getLocalizedMessage()); 
      io.setStackTrace(e.getStackTrace()); 
      throw io; 
     } 
     ssf_ = context.getSocketFactory(); 
    } 
    return ssf_; 
} 

및 CustomTrustManager.getTrustManagers()

static TrustManager[] getTrustManagers(String trustStoreFile, String trustStorePW) 
    throws NoSuchAlgorithmException, KeyStoreException 
{ 
    String alg = TrustManagerFactory.getDefaultAlgorithm(); 
    TrustManagerFactory tmFact = TrustManagerFactory.getInstance(alg); 
    tmFact.init((KeyStore)null); 
    TrustManager tms[] = tmFact.getTrustManagers(); 
    for(int i = 0; i < tms.length; i++) 
     if(tms[i] instanceof X509TrustManager) 
      tms[i] = new CustomTrustManager((X509TrustManager)tms[i]); 

    return tms; 
} 

static TrustManager[] getTrustManagers() 
    throws NoSuchAlgorithmException, KeyStoreException 
{ 
    return getTrustManagers(null, null); 
} 

의 코드 나는 어디에서나 보았지만 해결책을 찾아 낼 수없는 것 같습니다.

+0

아무것도 :

요청 방법의 변화는 내 코드를 고정 실행 다음은? 400을 리턴하는 서버이기 때문에, 그 쪽을 추적하는 것이 더 쉬울 수도 있습니다. –

+0

감사합니다, 서버 로그 실제로 오류를 해결하는 데 도움이 더 유용한 정보를 표시 않았다 –

답변

0

connection.setDoInput (true)가 내 RequestMethod를 버전 4에 게시하도록 설정했기 때문에 오류가 발생하여 서버에서 오류 요청을 반환하는 오류가 발생했습니다.

분명히 버전 2에서는이 기능이 설정되지 않으므로 버전 2에서는 제대로 작동하지 않습니다. 서버 로그에서

private String executeRequest(String urlAddress) 
{ 
    String responce = null; 
    String msg = null; 
    int error = 0; 
    try { 
     URL url = new URL(urlAddress); 
     HttpsURLConnection connection = (HttpsURLConnection)url.openConnection(); 
     SSLSocketFactory factory = SecureSocketFactory.getSSLSocketFactory(); 
     connection.setSSLSocketFactory(factory); 

     connection.setHostnameVerifier(new Verifier()); 

     if (method == RequestMethod.POST) 
     { 
      connection.setDoOutput(true); 
      connection.setRequestMethod("POST"); 
     } 
     else 
     { 
      connection.setDoInput(true); 
      connection.setRequestMethod("GET"); 
     } 
     msg = connection.getResponseMessage(); 
     error = connection.getResponseCode(); 
     if ("OK".equals(msg)) 
     { 
      InputStream content = (InputStream) connection.getContent(); 
      responce = convertStreamToString(content); 
     } 
     else 
     { 
      responce = "Error " + error; 
     } 
     connection.disconnect(); 

    } catch (Exception e) { 
     responce = e.toString(); 
    } 

    return responce; 
}