简体   繁体   中英

NTLM solution for RESTful web services in JAVA

I am writing a client for Restful web services.

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

I am getting unauthorized error 401. When I checked the server logs. I found the following.

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

So, I understood that I need to use NTLM authentication for this.

Can some one tell me how to modify this to do NTLM authentication.

Thanks in Advance.

Please check the below links

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

代替setCredentials尝试使用setProxyCredentials。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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