开发者

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

I'm having a very strange problem. I'm tryi开发者_如何学编程ng 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!


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜