0

我正在为 Restful 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();
        } 
    }

我收到未经授权的错误 401。当我检查服务器日志时。我发现了以下内容。

ERROR httpclient.HttpMethodDirector - Credentials cannot be used for NTLM authentication: org.apache.commons.httpclient.UsernamePasswordCredentials

所以,我知道我需要为此使用 NTLM 身份验证。

有人可以告诉我如何修改它以进行 NTLM 身份验证。

提前致谢。

4

2 回答 2

0

请检查以下链接

http://hc.apache.org/httpclient-3.x/authentication.html#NTLM http://davenport.sourceforge.net/ntlm.html

于 2013-01-15T12:34:41.587 回答
-1

而不是 setCredentials 尝试使用 setProxyCredentials。

client.getState().setProxyCredentials(AuthScope.ANY, defaultcreds);
于 2013-04-23T19:08:16.287 回答