Server returns 500 error only when called by Java client using urlConnection/httpUrlConnection

Posted by user455889 on Stack Overflow See other posts from Stack Overflow or by user455889
Published on 2011-02-15T06:55:00Z Indexed on 2011/02/15 7:25 UTC
Read the original article Hit count: 252

Hi -

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!

© Stack Overflow or respective owner

Related posts about java

Related posts about http