0

我正在使用 HttpsURLConnection 连接到我们的服务器,以便使用 post 方法获取数据。我面临的问题是我不时收到一个EOFException.

我已经进行了一些研究,并尝试使用例如connection.setRequestProperty("Connection", "close");,但是当我在其他属性中设置此属性时,我无法连接到服务器,总是收到此错误 java.net.SocketException: Socket is closed

现在我正在尝试使用解决这个问题,System.setProperty("http.keepAlive", "false");但我不完全确定我必须在哪里设置这个属性。我将它设置在连接到服务器的方法的开头。

在这里,您有我的代码中最相关的部分:

HttpsURLConnection conn = null;
    RESTResponse response = null;
    int status = -1;
    try {
        List<NameValuePair> params = request.getParameters();
        String uri = request.getRequestUri().toString();

        if (request.getMethod() == RESTMethods.GET) {
            if (params != null) {
                uri += "?";
                boolean first_param = true;

                for (NameValuePair p : params) {
                    if (first_param)
                        first_param = false;
                    else
                        uri += "&";

                    uri += p.getName() + "="
                            + URLEncoder.encode(p.getValue(), "UTF-8");
                }
            }
        }

        Security.addProvider(new BouncyCastleProvider());

        char[] passphrase = "mypass".toCharArray();
        try {
            KeyStore ksTrust = KeyStore.getInstance("BKS");
            ksTrust.load(context.getResources().openRawResource(R.raw.mystore),passphrase);

            TrustManagerFactory tmf = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());

            tmf.init(ksTrust);

            // Create a SSLContext with the certificate
            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, tmf.getTrustManagers(), new SecureRandom());
            Log.v(TAG,"URI = " + uri);
            URL url = new URL(uri);
            conn = (HttpsURLConnection) url.openConnection();

            conn.setSSLSocketFactory(sslContext.getSocketFactory());
            conn.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (CertificateException e) {
            e.printStackTrace();
        } catch (NotFoundException e) {
            e.printStackTrace();
        } catch (KeyStoreException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        }

        if (request.getHeaders() != null) {
            for (String header : request.getHeaders().keySet()) {
                for (String value : request.getHeaders().get(header)) {
                    conn.addRequestProperty(header, value);
                }
            }
        }

        switch (request.getMethod()) {
        case GET:
            conn.setDoOutput(false);
            break;
        case POST:
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded");
            conn.setRequestProperty("charset", "utf-8");
            conn.setRequestProperty("Connection", "close"); // disables connection reuse but getting ERROR


            DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
            wr.writeBytes(getQuery(params));
            wr.flush();
            wr.close();
        default:
            break;
        }

        try {               


            conn.connect();
            status = conn.getResponseCode(); //Receiving EOFException when Connection close not set
        } catch (IOException ex1) {
            //check if it's eof, if yes retrieve code again

                ex1.printStackTrace();
                // handle exception

        }

提前致谢!

4

0 回答 0