简体   繁体   English

如何使用JSoup或HttpConnection获取响应的确切状态代码?

[英]How to fetch the exact status code of the response using JSoup or HttpConnection?

Whenever I try to get the status code of a particular URL, both JSoup and HttpConnection always returns 200 even if there is a 301/302 redirect for the request URL. 每当我尝试获取特定URL的状态代码时,即使请求URL重定向为301/302,JSoup和HttpConnection都始终返回200。 Did anyone else face a similar issue and what should be the workaround to get the status code of the first request? 还有其他人遇到类似的问题吗?要获得第一个请求的状态码,应该采取什么解决方法?

The general strategy if you want to see redirection status codes is to turn off automatic redirect following. 如果要查看重定向状态代码,通常的策略是关闭自动重定向。 If you then want to follow the redirects, you can do it yourself ... by hand ... after making a note of the status code. 如果您随后要遵循重定向,则可以在记下状态代码之后自己动手操作。

The details will depend on what you are using; 详细信息将取决于您使用的内容。 eg HttpUrlConnection, Apache HttpClient v3, Apache HttpClient v4, or something else. 例如HttpUrlConnection,Apache HttpClient v3,Apache HttpClient v4或其他。

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

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