Java HTTP request sometimes freezes

In most cases, my HTTP requests work without problems. However, sometimes they will hang.

The code I use is configured so that if the request succeeds (with a response code of 200 or 201), then call screen.requestSucceeded (). If the request failed, call screen.requestFailed ().

When a request freezes, it does this before one of the above methods is called. Is there something wrong with my code? Should I use some good practice to prevent freezing?

Below is my code. I would appreciate any help. Thank!

HttpConnection connection = (HttpConnection) Connector.open(url
                    + connectionParameters);

            connection.setRequestMethod(method);
            connection.setRequestProperty("WWW-Authenticate",
                    "OAuth realm=api.netflix.com");
            if (method.equals("POST") && postData != null) {
            connection.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded");
            connection.setRequestProperty("Content-Length", Integer
                    .toString(postData.length));
            OutputStream requestOutput = connection.openOutputStream();
            requestOutput.write(postData);
            requestOutput.close();
        }
            int responseCode = connection.getResponseCode();
            System.out.println("RESPONSE CODE: " + responseCode);
            if (connection instanceof HttpsConnection) {
                HttpsConnection secureConnection = (HttpsConnection) connection;
                String issuer = secureConnection.getSecurityInfo()
                        .getServerCertificate().getIssuer();
                UiApplication.getUiApplication().invokeLater(
                        new DialogRunner(
                                "Secure Connection! Certificate issued by: "
                                        + issuer));

            }

            if (responseCode != 200 && responseCode != 201) {
                screen.requestFailed("Unexpected response code: "
                        + responseCode);
                connection.close();
                return;
            }

            String contentType = connection.getHeaderField("Content-type");
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            InputStream responseData = connection.openInputStream();
            byte[] buffer = new byte[20000];
            int bytesRead = 0;
            while ((bytesRead = responseData.read(buffer)) > 0) {
                baos.write(buffer, 0, bytesRead);
            }
            baos.close();
            connection.close();
            screen.requestSucceeded(baos.toByteArray(), contentType);
        } catch (IOException ex) {
            screen.requestFailed(ex.toString());
        }
+3
source share
4 answers

.

2 ,

System.setProperty("http.keepAlive", "false");

connection.setRequestProperty("Connection", "close");

Keep-alive - . .

+6

. , - . , , , .

- - , - , SO_TIMEOUT, 30 .

" ", , . Runnable exeuction, - ( - java, -). () 30 , . , . Runnable, .

+2

, "POST", , POST . - "POST", : connection.getResponseCode():

  • Content-Length
  • "Content-Type" ( )
  • get an OutputStream from a connection using connection.openOutputStream ()
  • write POST (form) data to OutputStream
  • close OutputStream
+1
source

I also noticed this problem on Blackberry OS 5.0. Unable to reproduce this reliably. We ended up using the extra thread using wait / notify along with Timertask.

+1
source

Source: https://habr.com/ru/post/1743263/


All Articles