0

This is the code I used :

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");
    }
}
}

And it gave 404 for the URL while that URL is working fine. Any reason why ?

4

2 に答える 2

2

「User-Agent」に適切なヘッダー値を追加します

connection.addRequestProperty("User-Agent", "Safari");
于 2012-07-03T11:04:24.510 に答える
0

CURL言っている:

curl -v http://www.amazon.co.jp/gp/seller/sell-your-stuff.html
* About to connect() to www.amazon.co.jp port 80 (#0)
*   Trying 176.32.120.128... connected
> GET /gp/seller/sell-your-stuff.html HTTP/1.1
> User-Agent: curl/7.23.1 (x86_64-pc-win32) libcurl/7.23.1 OpenSSL/0.9.8r zlib/1.2.5
> Host: www.amazon.co.jp
> Accept: */*
>
< HTTP/1.1 301 MovedPermanently

注意してくださいHTTP/1.1 301 MovedPermanently。受け取ったのですが、受け取っ404ていません301か? これは通常の Web 慣行です。301 ヘッダーは、コンテンツが別の場所に配置され、ユーザー (ブラウザー) がそこに移動する必要があることを意味します。

またHttpURLConnection、リダイレクトが許可されていることを確認してください。

于 2012-07-03T11:02:36.460 に答える