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

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?
Pra SysSoftware ProfessionalAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MishaProgrammerCommented:
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 ProfessionalAuthor Commented:
@Misha In my code snipped, where can I call getErrorStream()?
nociSoftware EngineerCommented:
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.
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

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

"NEGLECTED" ;)
Pra SysSoftware ProfessionalAuthor 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?
girionisCommented:
Do you have access to the server? What do server logs say?
CEHJCommented:
Will it be useful?
Possibly not ;)
Pra SysSoftware ProfessionalAuthor 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?
nociSoftware EngineerCommented:
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?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
girionisCommented:
@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 ProfessionalAuthor Commented:
@girionis - access.log shows same http response with error code 500. Nothing more. No other logs have any relevant information.
Pra SysSoftware ProfessionalAuthor 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.
girionisCommented:
@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 ProfessionalAuthor 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.
girionisCommented:
Is it a persistent error? Do you see any pattern? If not then I'd say it's a momentarily glitch.
nociSoftware EngineerCommented:
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 ProfessionalAuthor Commented:
Thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.