这是我使用的代码:
class ResponseCodeCheck
{
public static void main (String args[]) throws Exception
{
URL url = new URL("http://www.amazon.co.jp/gp/seller/sell-your-stuff.html");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
int code = connection.getResponseCode();
System.out.println("Response code of the object is "+code);
if (code==200)
{
System.out.println("OK");
}
}
}
当该 URL 工作正常时,它给出了 404 错误。 有什么理由吗?
最佳答案
为“User-Agent”添加正确的 header 值
connection.addRequestProperty("User-Agent", "Safari");
关于java - Java 中的 HTTP 响应代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11309170/