2

I'm having a very strange problem. I'm trying to call a servlet (JSP) with an HTTP GET and a few parameters (http://mydomain.com/method?param1=test&param2=123). If I call it from the browser or via WGET in a bash session, it works fine. However, when I make the exact same call in a Java client using urlConnection or httpURLConnection, the server returns a 500 error.

I've tried everything I have found online including:

urlConn.setRequestProperty("Accept-Language", "en-us,en;q=0.5");

Nothing I've tried, however, has worked. Unfortunately, I don't have access to the server I'm calling so I can't see the logs.

Here's the latest code:

private String testURLConnection() {
String ret = "";

String url = "http://localhost:8080/TestService/test";
String query = "param1=value1&param2=value2";

try {
    URLConnection connection = new URL(url + "?" + query).openConnection();
    connection.setRequestProperty("Accept-Charset", "UTF-8");
    connection.setRequestProperty("Accept-Language", "en-us,en;q=0.5");

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

    String line;

    StringBuilder content = new StringBuilder();
    while ((line = bufferedReader.readLine()) != null) {
        content.append(line + "\n");
    }
    bufferedReader.close();
    metaRet = content.toString();
    log.debug(methodName + " return = " + metaRet);
} catch (Exception ex) {
    log.error("Exception: " + ex);
    log.error("stack trace: " + getStackTrace(ex));
}

return metaRet;

}

Any help would be greatly appreciated!

5
  • Can you post all the relevant code that you are using to send the HTTP request to the server? Commented Feb 15, 2011 at 6:57
  • Thanks for your quick response. I updated my original posting with code. Commented Feb 15, 2011 at 7:21
  • It's a server side error, print your request URL in program with params and paste that in browser. It's likely that you are not using correct query string. Commented Feb 15, 2011 at 7:24
  • Hi Nishant - Thanks for your reply. I have done exactly that, and the URL works fine. Commented Feb 15, 2011 at 7:26
  • This might be caused by URLConnection sending an invalid Accept header field - try to override it with setReqquestProperty("Accept", "/"); Commented Oct 18, 2013 at 8:35

2 Answers 2

1

Unfortunately, I don't have access to the server I'm calling so I can't see the logs.

Unfortunately, the server logs would be the best place to look for info on what is causing a 500 Internal Error.

There may also be some information in the body of the response; e.g. an HTML formatted error page containing and error message and (if you are lucky) a formatted stack trace.

Absent that, you may have to resort to dumping the request headers for the cases that work and the cases that don't work and try to figure out what is different about them. Then, eliminate the differences by trial and error until you find the one(s) that cause the problem.


The other way to look at this is that it is the server's fault, not the client's. A server should be able to handle any request that the client throws at it. A 500 response is saying that the server cannot.

But off course, saying "it is the server's fault" doesn't help you solve the problem.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, Stephen - I fear it may take that.
0

I had the same problem, but fortunately i had access to server logs. This is the problem with server's jetty configuration.

The server would be giving this exception: java.lang.IllegalStateException: Form too large1105723>200000

This fix is in a jetty configuration on the server side.

From client side, you can try sending data in some other form other than application/www-x-formencoded if your server allows.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.