Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Https Web服务请求在旧版本的android(2.3.3)中工作,但在更高版本(4.0.3、4.3)中不工作_Https_Webservice Client_Android Version - Fatal编程技术网

Https Web服务请求在旧版本的android(2.3.3)中工作,但在更高版本(4.0.3、4.3)中不工作

Https Web服务请求在旧版本的android(2.3.3)中工作,但在更高版本(4.0.3、4.3)中不工作,https,webservice-client,android-version,Https,Webservice Client,Android Version,在为使用web服务的android应用程序工作时,我在尝试检索android版本4.0.3和4.3中的一些数据时遇到了错误的请求(响应代码400)消息。然而,perculiar的问题是,当使用相同的代码发送相同的请求时,但在使用安卓2.3.3版的设备上,它可以毫无问题地工作。我也尝试过使用httpGet而不是HttpsURLConnection,虽然这对所有版本都有效,但它并没有提供解决方案,因为我需要增加安全性 我的代码如下: private String executeRequest(Str

在为使用web服务的android应用程序工作时,我在尝试检索android版本4.0.3和4.3中的一些数据时遇到了错误的请求(响应代码400)消息。然而,perculiar的问题是,当使用相同的代码发送相同的请求时,但在使用安卓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()的代码:

以及CustomTrustManager.getTrustManager()的代码

static TrustManager[]getTrustManagers(字符串trustStoreFile,字符串trustStorePW)
抛出NoSuchAlgorithmException、KeyStoreException
{
字符串alg=TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmFact=TrustManagerFactory.getInstance(alg);
tmFact.init((密钥库)null);
TrustManager tms[]=tmFact.getTrustManager();
对于(int i=0;i

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

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

服务器日志中没有任何内容?由于是服务器返回400,因此在这方面可能更容易跟踪。谢谢,服务器日志确实显示了更多有用的信息,这确实帮助我解决了错误
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);
}
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;
}