httpclient post 基本认证

httpclient post basic authentication

是否有比所描述的更简单的方法来设置 http 客户端以进行抢占式基本身份验证。 我必须 post 从我的 spring 引导服务到另一个外部服务,但这在我的尝试中是不可能的。 我这样做真是个错误。

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]

我试了很多方法都没有用。然后我决定联系 api 制造商,他们会告诉我这个 api 只有在我们允许的情况下才有效。获得许可后,他开始工作。问题的原因是权限