0

安らかなWebサービスにアクセスすると、許可されていないエラーが発生します。私のサンプルプログラムは次のようになります。

public static void main(String[] args){
        // Use apache commons-httpclient to create the request/response
        HttpClient client = new HttpClient();
        Credentials defaultcreds = new UsernamePasswordCredentials("aaa", "cdefg");
        client.getState().setCredentials(AuthScope.ANY, defaultcreds);


        GetMethod method = new GetMethod(
                "http://localhost:8080/userService/usersByID/1234");
        try {
            client.executeMethod(method);
            InputStream in = method.getResponseBodyAsStream();
            // Use dom4j to parse the response and print nicely to the output stream
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            StringBuilder out = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                out.append(line);
            }
            System.out.println(out.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } 
    }

私の資格情報は正しいです。私のWebサービスは基本Http認証を使用します。

認証の範囲に疑問があります。

client.getState().setCredentials(AuthScope.ANY, defaultcreds);

私の資格情報は正しいです。

誰かがこの問題を解決するのを手伝ってもらえますか?

ありがとう。

4

1 に答える 1

1

まずブラウザでURLを確認し、確認しますか?ここで述べたように

401エラーの修正-一般

Each Web Server manages user authentication in its own way. A security officer (e.g. a Web Master) at the site typically decides which users are allowed to access the URL. This person then uses Web server software to set up those users and their passwords. So if you need to access the URL (or you forgot your user ID or password), only the security officer at that site can help you. Refer any security issues direct to them.

If you think that the URL Web page *should* be accessible to all and sundry on the Internet, then a 401 message indicates a deeper problem. The first thing you can do is check your URL via a Web browser. This browser should be running on a computer to which you have never previously identified yourself in any way, and you should avoid authentication (passwords etc.) that you have used previously. Ideally all this should be done over a completely different Internet connection to any you have used before (e.g. a different ISP dial-up connection). In short, you are trying to get the same behaviour a total stranger would get if they surfed the Internet to the Web page.

If this type of browser check indicates no authority problems, then it is possible that the Web server (or surrounding systems) have been configured to disallow certain patterns of HTTP traffic. In other words, HTTP communication from a well-known Web browser is allowed, but automated communication from other systems is rejected with an 401 error code. This is unusual, but may indicate a very defensive security policy around the Web server.

手動修正

ブラウザからURLをヒットし、HTTPトラフィックを記録します(ヘッダー、本文)

Javaクライアントコードを実行し、HTTPトラフィックを記録します(Headers、body)

違いを分析して修正する

于 2013-01-15T10:30:42.793 に答える