How to find out specifics of 400 Http error in Jav

2020-04-21 04:17发布

I'm getting 400 errors trying to connect to a website.

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

标签: java
2条回答
▲ chillily
2楼-- · 2020-04-21 04:43

HTTP 400 is "Bad Request". 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. 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
  • Error codes that are too specific can cause security problems.

For sniffing HTTP traffic, I've used 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.)

Edit: Code is nice, but we really need to see the HTTP request itself in order to figure out what is wrong with it. Get Wireshark or Charles, or point your client at a socket that dumps the request to a file.

查看更多
Animai°情兽
3楼-- · 2020-04-21 04:47

400 means the request is malformed:

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.

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.

查看更多
登录 后发表回答