是否有比上述方法更简单的方法来设置HTTP客户端以进行抢占式基本身份验证。 我必须从我的spring boot服务发布到另一个外部服务,但这在我的尝试中是不可能的。 我在这样做时犯了个错误。
CredentialsProvider provider = new BasicCredentialsProvider();
usernamePasswordCredentials credentials = new usernamePasswordCredentials("user1","user1Pass");
provider.setCredentials(AuthScope.ANY,credentials);
HttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();
HttpPost httpPost = new HttpPost("http://91.204.239.42:8083/broker-api/send");
httpPost.setHeader("Content-type","application/json");
try {
httpPost.setEntity(new StringEntity(json));
HttpResponse response = client.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
return statusCode;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (Clientprotocolexception e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return 500;
此堆栈跟踪
org.apache.http.conn.httphostconnectexception: Connect to 91.204.239.42:8083 [/91.204.239.42] failed: Connection refused: connect
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:156) ~[httpclient-4.5.9.jar:4.5.9]
at org.apache.http.impl.conn.PoolingHttpClientConnectionmanager.connect(PoolingHttpClientConnectionmanager.java:374) ~[httpclient-4.5.9.jar:4.5.9]