简体   繁体   English

如何找出Java中400 Http错误的详细信息?

[英]How to find out specifics of 400 Http error in Java?

I'm getting 400 errors trying to connect to a website. 我在尝试连接到网站时遇到400个错误。

How do I set the connection to display why the server is rejecting me? 如何设置连接以显示服务器拒绝我的原因?

try {
doc = Jsoup.connect(URL).timeout(10 * 30000).get();
} catch (IOException ioe) {
System.out.println("Error:" + ioe);
}

http://jsoup.org/apidocs/org/jsoup/Connection.html http://jsoup.org/apidocs/org/jsoup/Connection.html

HTTP 400 is "Bad Request". HTTP 400是“错误请求”。 This could be a problem with the server, but it's probably a problem in your code . 这可能是服务器的问题,但可能代码中的问题。 The best way to debug these things doesn't depend on the language you use: simply sniff the network traffic to see what your HTTP request looks like. 调试这些问题的最佳方法并不取决于您使用的语言:只需嗅探网络流量即可查看HTTP请求的外观。 You can also see the server's reply, to see if it gives any more information. 您还可以查看服务器的答复,以查看其是否提供更多信息。 Usually the server is fairly vague, for two reasons. 通常,服务器相当模糊,有两个原因。

  • People writing HTTP servers aren't setting out to help people debug HTTP clients, and 编写HTTP服务器的人并未着手帮助人们调试HTTP客户端,并且
  • Error codes that are too specific can cause security problems. 太具体的错误代码可能会导致安全问题。

For sniffing HTTP traffic, I've used Wireshark; 为了嗅探HTTP流量,我使用了Wireshark。 Charles also works, and there are plenty of others. 查尔斯也工作,还有很多其他人。

(If you just want to display the server's reason for the error, just display the HTTP response like any other.) (如果只想显示服务器的错误原因,则像其他任何一个一样显示HTTP响应。)

Edit: Code is nice, but we really need to see the HTTP request itself in order to figure out what is wrong with it. 编辑:代码很不错,但是我们真的需要查看HTTP请求本身,才能弄清楚它出了什么问题。 Get Wireshark or Charles, or point your client at a socket that dumps the request to a file. 获取Wireshark或Charles,或将您的客户端指向将请求转储到文件的套接字。

400 means the request is malformed : 400表示请求格式错误

The request could not be understood by the server due to malformed syntax. 由于语法格式错误,服务器无法理解请求。 The client SHOULD NOT repeat the request without modifications. 客户端不应该在没有修改的情况下重复请求。

Fundamentally, what you need to do is form the request correctly. 从根本上讲,您需要做的是正确形成请求。 For instance, if you're sending request parameters and usual the unusual encoding, make sure they're all correctly URI-encoded . 例如,如果您要发送请求参数,并且通常使用非常规编码,请确保它们均已正确进行URI编码

How do I set the connection to display why the server is rejecting me? 如何设置连接以显示服务器拒绝我的原因?

Usually the body of the response will include more detail. 通常,响应的主体将包含更多细节。 Not always, it's not required, but it could provide insight into why the request was considered "malformed." 并非总是如此,不是必需的,但是它可以提供深入了解为什么请求被视为“格式错误”的原因。 If the body of the response does not include this information, there's no way other than just looking at the request you're sending to figure out why it's considered malformed. 如果响应的正文中不包含此信息,则只能查看您发送的请求以弄清为什么它被认为格式错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM