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();
}
}
}
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();
}
}
}
相关推荐
84487600 2020-08-16
标题无所谓 2020-06-14
yanghui0 2020-06-09
似水流年梦 2020-08-09
knightwatch 2020-07-26
fengchao000 2020-06-16
sicceer 2020-06-12
yanghui0 2020-06-09
创建一个 HttpClient 实例,这个实例需要调用 Dispose 方法释放资源,这里使用了 using 语句。接着调用 GetAsync,给它传递要调用的方法的地址,向服务器发送 Get 请求。
wanghongsha 2020-06-04
jiaguoquan00 2020-05-26
zhaolisha 2020-05-16
wanghongsha 2020-05-05
wanghongsha 2020-04-14
knightwatch 2020-04-11
hygbuaa 2020-03-27
zergxixi 2020-03-24