0

I have a problem when I run my code, after opening HTTP request i'm trying to retrieve the response code, but then the program stop running on print statement Below is my code:

private static HttpURLConnection openHttpConnection(URL url, String method){
try {
HttpURLConnection connection = null;
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod(method);
connection.setRequestProperty("Content-Type", 
"application/json;charset=UTF-8");
connection.setRequestProperty("Accept", "application/json");
System.out.println(connection.getResponseCode());
}
catch (Exception exception) { 
exception.printStackTrace();
}

the program stopped in last line, the problem appear when I open many HTTP requests after each other. Note: when I debug the same code it works with dubbing but not with running. Also the problem appear after 8 HTTP requests.

Please help. Thank you.

  • You say that your program hangs after 8 Requests. Do you at some point close your connections? Are they really closed (at the OS level)? If your connections are still open/hanging, the server (whatever you connect to) might run out of connections.... – sruetti Jun 06 '17 at 09:15

3 Answers3

0

Did you put these lines of code inside a try/catch block? An exception might be raised, catching it to get the stacktrace would be helpful to get information. Besides, it would be more convenient to have more information about the execution context : what kind of application is this, where does the url come from, and so on.

Javert0
  • 11
  • 2
  • Yes I put it inside try/catch block, the url given as parameter for the method, i updated the code above.. thank you. – Besh Mohammed Jun 01 '17 at 07:55
  • In rare cases, there is no Exception thrown, but a Throwable. A solution might be to catch it, but it is not recommended (https://stackoverflow.com/questions/6083248/is-it-a-bad-practice-to-catch-throwable). You would have more information about what happens, then. IMHO, you could catch it temporarily. – Javert0 Jun 01 '17 at 08:18
  • I put Throwable block, but the problem still occurring. – Besh Mohammed Jun 01 '17 at 09:26
0
  • If your program stops on the last line (i.e. the JVM exits) without printing any response code, an exception must have been thrown by the getResponseCode(). This Exception will most likely tell you what the problem is. As Javert0 points out, the place you find this exception depends on your environment - worst case catch any exception and print it yourself...

  • If your program hangs on the last line, connection.getResponseCode() might not get a response from the server and wait for a timeout.

  • As you don't have an explicit connection.connect() before asking for the response code (connection.getResponseCode()), the Problem can be almost anything: a timeout (unresponsive Server / Network / DNS / ...), an invalid result, missing permissions, ...

sruetti
  • 532
  • 2
  • 7
  • I put catch block, but nothing thrown there. – Besh Mohammed Jun 01 '17 at 07:57
  • Are you sure that `connection.getResponseCode()` gets called? Does your code exit on `getResponseCode()` i.e. exit the JVM or does it hang (wait for a timeout / wait indefinitely)? – sruetti Jun 06 '17 at 09:07
-1

Server might be sending garbage ... or simply closing the connection. that time it's occurred error.

Or some time response is not come or null at that time it's make error.

https://nazimkuet.wordpress.com/2015/03/16/how-to-get-http-response-code-for-a-url-in-java/

Firstly you check your response either you have write your error.