仅当 Java 客户端使用 urlConnection/httpUrlConnection 调用时,服务器返回 500 错误
我有一个非常奇怪的问题。我正在尝试使用 HTTP GET 和一些参数调用 servlet (JSP) (http://mydomain.com/method?param1=test¶m2=123)。如果我从浏览器或在 bash 会话中通过 WGET 调用它,它就可以正常工作。但是,当我使用 urlConnection 或 httpURLConnection 在 Java 客户端中进行完全相同的调用时,服务器返回 500 错误。
我已经尝试了在网上找到的所有内容,包括:
urlConn.setRequestProperty("Accept-Language", "en-us,en;q=0.5");
但是,我尝试过的一切都不起作用。不幸的是,我无权访问我正在调用的服务器,因此我看不到日志。
这是最新的代码:
private String testURLConnection() {
String ret = "";
String url = "http://localhost:8080/TestService/test";
String query = "param1=value1¶m2=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;
}
任何帮助将不胜感激!
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¶m2=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¶m2=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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的是,服务器日志是查找导致 500 内部错误的原因信息的最佳位置。
响应正文中还可能包含一些信息;例如,包含错误消息的 HTML 格式的错误页面和(如果幸运的话)格式化的堆栈跟踪。
如果没有,您可能不得不转储有效情况和无效情况的请求标头,并尝试找出它们的不同之处。然后,通过反复试验消除差异,直到找到导致问题的差异。
从另一种角度来看,这是服务器的错误,而不是客户端的错误。服务器应该能够处理客户端向其发出的任何请求。 500 响应表示服务器不能。
但当然,说“这是服务器的错”并不能帮助你解决问题。
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.
我遇到了同样的问题,但幸运的是我可以访问服务器日志。这是服务器jetty配置的问题。
服务器将给出此异常:
java.lang.IllegalStateException:表单太大1105723>200000
此修复位于服务器端的码头配置中。
从客户端,如果服务器允许,您可以尝试以 application/www-x-formencoded 以外的其他形式发送数据。
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.