【问题标题】:Web service request working in old version of android (2.3.3) but not in later versions (4.0.3, 4.3)Web 服务请求在旧版本的 android (2.3.3) 中有效,但在更高版本 (4.0.3、4.3) 中无效
【发布时间】:2013-09-03 09:46:21
【问题描述】:

在开发使用 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);
}

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

【问题讨论】:

  • 服务器日志中没有内容?由于是服务器返回 400,因此在该端进行跟踪可能更容易。
  • 谢谢,服务器日志确实显示了更多有用的信息,这确实帮助我解决了错误

标签: https webservice-client android-version


【解决方案1】:

我发现了我的错误,因为做 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-24
    • 2019-03-09
    相关资源
    最近更新 更多