httpclient实现ntlm认证(window)

httpclient版本 4.5

写道
import java.nio.charset.Charset;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.NTCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;


public class SCORunbookTest {
private void test() throws Exception {
CredentialsProvider credsProvider = new BasicCredentialsProvider();

NTCredentials creds = new NTCredentials("user", "password", "", "");
credsProvider.setCredentials(new AuthScope("10.0.10.76", 81, null, "ntlm"), creds);
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.build();
try {
HttpHost target = new HttpHost("10.0.10.76", 81, "http");
// 保证相同的内容来用于执行逻辑相关的请求
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("/Orchestrator2012/Orchestrator.svc/");
CloseableHttpResponse response = httpclient.execute(target, httpGet, localContext);
try {
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
System.out.println(EntityUtils.toString(response.getEntity(), "utf-8"));

EntityUtils.consume(response.getEntity());
} finally {
response.close();
}
} finally {
httpclient.close();
}
}
}

 

你可能感兴趣的:(httpclient)