웹 서비스를 사용하는 안드로이드 용 응용 프로그램에서 작업하는 동안 잘못된 요청 (응답 코드 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);
}
의 코드 나는 어디에서나 보았지만 해결책을 찾아 낼 수없는 것 같습니다.
아무것도 :
요청 방법의 변화는 내 코드를 고정 실행 다음은? 400을 리턴하는 서버이기 때문에, 그 쪽을 추적하는 것이 더 쉬울 수도 있습니다. –감사합니다, 서버 로그 실제로 오류를 해결하는 데 도움이 더 유용한 정보를 표시 않았다 –