views:

38

answers:

1

I'm trying to connect to a web page through a HttpURLConnection but it's not responding the same as a regular browser (firefox, chrome). I'm getting an error 500.

With the same code I can get "anyother" page (google, for example). My code is posted below, but I'm pretty sure it is ok.

Using "Live HTTP Headers" firefox addon I sent the same request and received a valid response.

//...
String urlConsulta = "myURL";
URL url = new URL(urlConsulta);
HttpURLConnection uConsulta = (HttpURLConnection)consulta.openConnection();
uConsulta.setDoOutput(true);
uConsulta.setRequestMethod("POST");
uConsulta.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");
uConsulta.addRequestProperty("Host", "host");
//...
+1  A: 

This can be caused by everything. The server may rely on the presence of certain request parameters. The server may rely on a valid session. The server may sniff the user agent. Etc..etc.. Read the server logs for the cause of the error. Or if you don't have access to server logs, read the body of the error response by HttpURLConnection#getErrorStream(), it may contain the error details.

See also:

BalusC
Thanks for the quick answer. Sorry about the lack of information, I tried to put just the essential and might have forgotten other important things. There is no need of valid session, I've tried several different user-agents. I am passing all needed params. Actually you can type exactly the same web address used as "myURL" in the small piece of code I posted in any regular browser and get a valid answer.
The actual answer is in the server logs and/or the error response body. Go read it. If the `500` is coming from a Servlet (you tagged `servlets`), then it simply means that it has thrown an `Exception`. In other words, it's possibly a plain vanilla bug in the Servlet code. If you have full control over it, read the logs and fix it accordingly.
BalusC