Java http客户端4.3不发送凭据
我试图使用ApacheHTTP客户机4.3发送get请求(到使用自签名证书的客户机),但是每次都返回错误“需要身份验证”。在web浏览器中,它工作正常,因此用户名/密码/url是正确的。这不是使用http客户端4.3传递用户名/密码的方法吗Java http客户端4.3不发送凭据,java,https,apache-httpclient-4.x,Java,Https,Apache Httpclient 4.x,我试图使用ApacheHTTP客户机4.3发送get请求(到使用自签名证书的客户机),但是每次都返回错误“需要身份验证”。在web浏览器中,它工作正常,因此用户名/密码/url是正确的。这不是使用http客户端4.3传递用户名/密码的方法吗 public static String sendJsonHttpGetRequest( String host, String path, String use
public static String sendJsonHttpGetRequest(
String host,
String path,
String username,
String password,
int socketTimeout,
int connectionTimeout,
int connectionRequestTimeout
) throws Exception
{
String responseBody = null;
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustStrategy(){
@Override
public boolean isTrusted(java.security.cert.X509Certificate[] chain, String authType)
throws java.security.cert.CertificateException
{
return true;
}
});
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).setDefaultCredentialsProvider(credsProvider).build();
URIBuilder uriB = new URIBuilder().setScheme("https").setHost(host).setPath(path);
HttpGet _http = new HttpGet( uriB.build() );
RequestConfig _requestConfig = RequestConfig.custom().
setSocketTimeout(socketTimeout).
setConnectTimeout(connectionTimeout).
setConnectionRequestTimeout(connectionRequestTimeout).build();
_http.addHeader("Content-Type", "application/json");
_http.addHeader("Accept","application/json, text/xml;q=9, /;q=8");
_http.setConfig(_requestConfig);
// ###########################
ResponseHandler<String> response = new BasicResponseHandler();
responseBody = httpclient.execute(_http, response);
return responseBody;
}
公共静态字符串sendJsonHttpGetRequest(
字符串主机,
字符串路径,
字符串用户名,
字符串密码,
int socketTimeout,
int connectionTimeout,
int连接请求超时
)抛出异常
{
字符串responseBody=null;
CredentialsProvider credsProvider=新的BasicCredentialsProvider();
setCredentials(AuthScope.ANY,新用户名密码Credentials(用户名,密码));
SSLContextBuilder=新的SSLContextBuilder();
builder.loadTrustMaterial(空,新TrustStrategy(){
@凌驾
已信任公共布尔值(java.security.cert.X509Certificate[]链,字符串authType)
抛出java.security.cert.CertificateException
{
返回true;
}
});
SSLConnectionSocketFactory sslsf=新的SSLConnectionSocketFactory(builder.build());
CloseableHttpClient httpclient=HttpClients.custom().setSSLSocketFactory(sslsf).setDefaultCredentialsProvider(CredProvider).build();
URIBuilder uriB=new URIBuilder().setScheme(“https”).setHost(主机).setPath(路径);
HttpGet _http=newhttpget(uriB.build());
RequestConfig\u RequestConfig=RequestConfig.custom()。
设置socketTimeout(socketTimeout)。
设置连接超时(connectionTimeout)。
setConnectionRequestTimeout(connectionRequestTimeout).build();
_addHeader(“内容类型”、“应用程序/json”);
_addHeader(“Accept”,“application/json,text/xml;q=9,/;q=8”);
_http.setConfig(_requestConfig);
// ###########################
ResponseHandler response=new BasicResponseHandler();
responseBody=httpclient.execute(_-http,response);
返回响应体;
}
我自己不使用这个库,但是您尝试过HttpClient
类吗
HttpClient client = new HttpClient();
client.getParams().setAuthenticationPreemptive(true);
client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
GetMethod method = new GetMethod(uri);
client.executeMethod(method);
您仍然需要构建uri并设置超时,但这可能是一个选项。现在使用http 4+时,您必须在两个位置提供它才能工作 二是
authCache.put(host, basicAuth);
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
HttpClientContext _context = HttpClientContext.create();
_context.setAuthCache(authCache);
_context.setCredentialsProvider(credentialsProvider);
responseBody = httpclient.execute(_http, response, _context);