2

在开发使用 Web 服务的 android 应用程序时,我在尝试检索 android 版本 4.0.3 和 4.3 中的某些数据时遇到了错误请求(响应代码 400)消息。然而,奇怪的是,当使用相同的代码发送相同的请求但在使用 android 版本 2.3.3 的设备上时,它可以正常工作。我也尝试过使用 httpGet 而不是 HttpsURLConnection,虽然这适用于所有版本,但它不提供解决方案,因为我需要增加安全性。

我的代码如下:

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);
}

我到处找,但似乎找不到解决方案,请帮忙。

4

1 回答 1

0

我发现了我的错误,因为做 connection.setDoInput(true) 它静默地将我的 Requestmethod 设置为在版本 4 中发布,这在服务器上给出了一个错误,导致它返回错误的请求。

显然它没有在版本 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;
}
于 2013-09-10T08:00:20.300 回答