0

I have a server with SSL certificate.
And I am using functions uploadFileHttps(...) to upload an image and get corresponding response with HttpsUrlConnection.
For the upload part, it sent out the image data through output stream.
However, server haven't received that image.
Also, when it is trying to obtain response from server through input stream. It catches an exception "java.net.SocketTimeoutException: Read timed out"
How could I fix it?

private static final int CONNECTION_TIMEOUT = 20000;
private static final int IMAGE_UPLOAD_TIMEOUT = 60000;

private void uploadFileHttps(File file, String requestURL) {
    HttpsURLConnection connection = null;
    try {

        //requestURL is a https link like: https://test.abcde.com/upload
        connection = (HttpsURLConnection) new URL(requestURL).openConnection();

        connection.setHostnameVerifier(new NullHostNameVerifier());

        connection.setConnectTimeout(CONNECTION_TIMEOUT);//timeout for handshake
        connection.setReadTimeout(IMAGE_UPLOAD_TIMEOUT);//timeout for waiting read data
        connection.setRequestMethod("PUT");
        connection.setRequestProperty("Content-Type", "image/jpeg");
        Token.setAuthTokenHeader(connection, context);

        connection.setDoOutput(true);//set to use httpURLConnection for output
        connection.connect();

        //upload file to server
        DataOutputStream out = new DataOutputStream(connection.getOutputStream());
        int bytesRead;
        byte buf[] = new byte[1024];
        BufferedInputStream bufInput = new BufferedInputStream(new FileInputStream(file));
        while ((bytesRead = bufInput.read(buf)) != -1) {
            // write output
            out.write(buf, 0, bytesRead);
            out.flush();
        }
        out.flush();
        out.close();

        //get response from server
        BufferedReader br = new BufferedReader(new InputStreamReader((connection.getInputStream())));
        StringBuilder sb = new StringBuilder();
        String output;
        while ((output = br.readLine()) != null) {
            sb.append(output);
        }
        System.out.println(sb.toString());
    } catch (Exception e) {
        if (e != null)
            e.printStackTrace();
    } finally {
        if (connection != null) connection.disconnect();
    }
}

class NullHostNameVerifier implements HostnameVerifier {

    @Override
    public boolean verify(String hostname, SSLSession session) {
        Log.i("RestUtilImpl", "Approving certificate for " + hostname);
        return true;
    }

}

Stack Trace for error

W/System.err﹕ java.net.SocketTimeoutException: Read timed out
W/System.err﹕ at com.android.org.conscrypt.NativeCrypto.SSL_read(Native Method)
W/System.err﹕ at com.android.org.conscrypt.OpenSSLSocketImpl$SSLInputStream.read(OpenSSLSocketImpl.java:689)
W/System.err﹕ at java.io.InputStream.read(InputStream.java:162)
W/System.err﹕ at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:142)
W/System.err﹕ at java.io.BufferedInputStream.read(BufferedInputStream.java:227)
W/System.err﹕ at com.android.okhttp.internal.Util.readAsciiLine(Util.java:316)
W/System.err﹕ at com.android.okhttp.internal.http.RawHeaders.fromBytes(RawHeaders.java:308)
W/System.err﹕ at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:135)
W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:644)
W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:347)
W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:179)
W/System.err﹕ at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:246)
W/System.err﹕ at com.seasonworkstation.testhttps.network.HttpRequestTask.uploadFileSSL(HttpRequestTask.java:355)
W/System.err﹕ at com.seasonworkstation.testhttps.network.HttpRequestTask.doRequest(HttpRequestTask.java:152)
W/System.err﹕ at com.seasonworkstation.testhttps.network.HttpRequestTask.doInBackground(HttpRequestTask.java:182)
W/System.err﹕ at com.seasonworkstation.testhttps.network.HttpRequestTask.doInBackground(HttpRequestTask.java:47)
W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
W/System.err﹕ at java.lang.Thread.run(Thread.java:841)
Season
  • 1,178
  • 2
  • 22
  • 42
  • The same issue http://stackoverflow.com/questions/15608499/getting-java-net-sockettimeoutexception-connection-timed-out-in-android – Dmitriy Puchkov Sep 18 '15 at 09:13
  • 1
    @gabber no, it's not same issue as the Exception is a Read Timed Out. – Season Sep 18 '15 at 10:00
  • Try putting `connection.connect();` between "upload file to server" part and "get response from server" part. – BNK Sep 19 '15 at 16:14

1 Answers1

0
connection.setRequestMethod("PUT");

This sets the request method to PUT.

connection.setDoOutput(true);//set to use httpURLConnection for output

This sets the request method to POST. You need to execute this before the PUT line above.

user207421
  • 305,947
  • 44
  • 307
  • 483