Connection already established exception in HttpsURLConnection

I am trying to execute a POST request using HttpsURLConnection and get

 java.net.ProtocolException: Connection already established 

on setRequestMethod Oddly enough, conn.connected returns false only earlier.

What am I missing?

 URL url = new URL("https://ws.audioscrobbler.com/2.0/"); HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); conn.setReadTimeout(15000); conn.setConnectTimeout(15000); // here conn.connected is false conn.setRequestMethod("POST"); // here I get java.net.ProtocolException: Connection already established conn.setDoInput(true); 

EDIT

I tried DefaultHttpClient and it works, so I will use it instead.

+4
source share
2 answers

I tried DefaultHttpClient and it works, so I will use it instead.

0
source

For everyone who is faced with this problem, I had an order of operations that affected me only when I was doing HTTP POST, which had content in the request body. This is not entirely clear in all scenarios when the HttpURLConnection actually initiates a connection to the server.

My initial request looked like this:

  HttpURLConnection conn = null; try { conn = (HttpURLConnection) baseUrl.openConnection(); conn.setConnectTimeout(connectTimeoutMillis); conn.setReadTimeout(requestTimeoutMillis); //required for reading a response body conn.setDoInput(true); //Not all request types have a body (eg GET usually doesn't) if(requestBody != null && requestBody.length > 0) { conn.setDoOutput(true); conn.setFixedLengthStreamingMode(requestBody.length); conn.getOutputStream().write(requestBody); conn.getOutputStream().flush(); conn.getOutputStream().close(); } try { conn.setRequestMethod(verb.toUpperCase()); } catch (final ProtocolException e) { response.setError("Invalid HTTP verb \"" + verb + "\" received.",""); Log.e(TAG, response.errorMessage, e); return response; } 

It turns out you cannot call β€œconn.setRequestMethod (...)” after you called β€œconn.getOutputStream ()”, so in my case, a simple fix was calling β€œconn.setRequestMethod (...)” before than contact a letter to the requesting authority. Work code:

 HttpURLConnection conn = null; try { conn = (HttpURLConnection) baseUrl.openConnection(); conn.setConnectTimeout(connectTimeoutMillis); conn.setReadTimeout(requestTimeoutMillis); //required for reading a response body conn.setDoInput(true); try { conn.setRequestMethod(verb.toUpperCase()); } catch (final ProtocolException e) { response.setError("Invalid HTTP verb \"" + verb + "\" received.",""); Log.e(TAG, response.errorMessage, e); return response; } //Not all request types have a body (eg GET usually doesn't) if(requestBody != null && requestBody.length > 0) { conn.setDoOutput(true); conn.setFixedLengthStreamingMode(requestBody.length); conn.getOutputStream().write(requestBody); conn.getOutputStream().flush(); conn.getOutputStream().close(); } 

The only real change is simply switching the order of the calls and eliminating the exception. Hope this solves the problem for everyone who has this problem.

+1
source

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


All Articles