HttpURLConnection getInputStream throws java.io.IOException HTTP response code: 500

Pra Sys
Pra Sys used Ask the Experts™
on
HttpURLConnection getInputStream throws below errors sometimes -

java.lang.RuntimeException: java.io.IOException: Server returned HTTP response code: 500

Open in new window


Here is the part of callstack -

09:05:02,198 ERROR [stderr] (http-/111.222.333.444:11800) java.io.IOException: Server returned HTTP response code: 500 for URL: https://111.222.333.444:11800/xyz/myapp/3.0/urn:name:ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJ
09:05:02,198 ERROR [stderr] (http-/111.222.333.444:11800)   at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1628)
09:05:02,199 ERROR [stderr] (http-/111.222.333.444:11800)   at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)

Open in new window


This is a how my code looks like. This is not a client code. This runs on server like Tomcat -

private void getResult(HttpURLConnection urlconnection,
            String result) {
        BufferedReader bufferedReader;
        String line;
        try {

            bufferedReader = new BufferedReader(new InputStreamReader(
                    urlconnection.getInputStream()));

            while ((line = bufferedReader.readLine()) != null) {
                result += line;
            }

            bufferedReader.close();
        } catch (Exception ex) {
            String error = String.format("Invalid InputStream, %s",
                    ex.getMessage());
        }

        return;

}

Open in new window


The URL that I have prepared looks like below -

 https://111.222.333.444:11800/xyz/myapp/3.0/urn:name:ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJ

Open in new window


Can this be java problem or server is responsible for this? What could be the problem?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
This error mean, than your "server encountered an unexpected condition which prevented it from fulfilling the request. "
That why you can try  to read stream error with this method
public InputStream getErrorStream()

Open in new window

https://docs.oracle.com/javase/6/docs/api/java/net/HttpURLConnection.html#getErrorStream%28%29
And than you can get more information about this error.
Also try to recompiled your code, may be there is some changes, that has not been recompiled.
Pra SysSoftware Professional

Author

Commented:
@Misha In my code snipped, where can I call getErrorStream()?
nociSoftware Engineer
Distinguished Expert 2018
Commented:
The error 500 says there is a problem on the Server.... It got a request and could not handle that.
(f.e. it started a CGI sub process that died unexpectedly, there is a reverse proxy running on the server that could not connect to the backend..., a script needed to resolve teh request cannot be run.

THat might be because you asked the wrong question or failure to handle the error more gracefully on the server side.
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Top Expert 2016

Commented:
where can I call getErrorStream()?
Right after you call getInputStream()

"NEGLECTED" ;)
Pra SysSoftware Professional

Author

Commented:
@CEHJ Should we call it at start of the method or in catch block? Will it be useful? Will we get to see what exact error there is?
Distinguished Expert 2018

Commented:
Do you have access to the server? What do server logs say?
Top Expert 2016

Commented:
Will it be useful?
Possibly not ;)
Pra SysSoftware Professional

Author

Commented:
@girionis - access.log from JBoss was not very useful. It did show lot of 500 errors but no details.
@CEHJ - yes. Possibly no use .

How does server reacts to such exception? Does is closes connection? How does it reconnects automatically?

What all possible reasons for this exception? Can we do something programatically to prevent these exceptions? Anything can be done on webserver?

In my question, 111.222.333.444:11800 is a different web server. My web server will call url from this webserver, internally getResult() gets called. Does this exception shows any connectivity issues between two servers? Or bad http request/response?
Software Engineer
Distinguished Expert 2018
Commented:
The server returns the HTTP answer:
500 - Internal Error

and either closes the connection or waits for the next item request. (depending on server config and wether HTTP/2 or HTTP/1.1 or HTTP/1.0 is used (with/without KeepAlive etc.).

A 500 response is the server ACTIVELY telling the client that it failed in fulfilling the request... And it is not a security violation as such, (that would be a 401) it isn't not found (404) there are no redirects (301/302).. etc.and it definitely wasn't succesful (200).
500 responses mostly are about the server running into something unknown, or the request was started but then timed out, or a backend stopped responding... (The latter can also be read as the backend wasn't quick enough to respond).

Proxies mostly show this kind of error with too slow servers... say the proxy has a 1 minute timeout, and the server takes >1 minute to get the data..., then the proxy (the part your program talks to as server) will return to at least mention that a backend fails....

Does JBOSS have an errorlog does the errorlog have entries corresponding in time with the 500 errors.
Does the server have anything in the systemlogs (Unix: /var/log/messages /var/log/daemon etc.; Windows: Enventvwr ) showing some correlation?
Distinguished Expert 2018

Commented:
@girionis - access.log from JBoss was not very useful. It did show lot of 500 errors but no details.

What about the server.log? Or any other application log files?
Pra SysSoftware Professional

Author

Commented:
@girionis - access.log shows same http response with error code 500. Nothing more. No other logs have any relevant information.
Pra SysSoftware Professional

Author

Commented:
@noci - I will check files under /var/log folder where JBoss is installed and will get back. But as per various responses above, I am ruling out programming error. I dont think we have any way programmatically to prevent this error. Let me know if this conclusion is wrong.
Distinguished Expert 2018

Commented:
@girionis - access.log shows same http response with error code 500. Nothing more. No other logs have any relevant information.

Do you not have any other log files? JBoss has a server.log, does it not say anything there? Is there no application logs?
Pra SysSoftware Professional

Author

Commented:
We got network log checked. They indicated Apache not been able to connect to backend server, ip mentioned in URL. Hence, apache not able to send request to that server and while expecting response getting null and 500 error. Could this be momentary glitch in network at apache server location or backend server? These two machines are located in different cities.
Distinguished Expert 2018

Commented:
Is it a persistent error? Do you see any pattern? If not then I'd say it's a momentarily glitch.
nociSoftware Engineer
Distinguished Expert 2018
Commented:
Could be a momentary glitch (like reboot of backend server, or services down there for some reason.
Overload of backend might be an other cause which might be an irregular recurring issue.
If persistent with many queries then probably a mis-configuration of some kind.

error 500 is a frontend server error indicating some backend action failed.
Pra SysSoftware Professional

Author

Commented:
Thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial