Java org.apache.http.conn.HttpHostConnectException:连接到URL失败:连接被拒绝:连接

时间:2017-03-20 12:30:43

标签: apache-commons-httpclient

我正在尝试通过Httpclient从API获取响应,但我收到org.apache.http.conn.HttpHostConnectException: Connect to URL failed: Connection refused: connect错误。

我能够使用PoSTMAN工具获得响应,但是即使在设置代理后我通过office域访问时也无法通过ecclipse获得响应。

请注意,我也可以在没有代理的情况下在域外获得响应。另请注意,内部域的几个URL工作正常,但我遇到了生产URL的问题。我正在使用Eclipse。你能告诉我如何解决这个问题吗?我遇到了问题。

提前谢谢

My code is as below:

<!-- language: java -->  

    public class getResponse {

    private static String url = "https://example.com/abc/v1/Region_API(60000108915980)";

    public static void main(String[] args)throws Exception {

    CloseableHttpClient httpclient = HttpClientBuilder.create().build();

    try {

    HttpGet get = new HttpGet(url);
    String basicAuth = "Basic " + new String(new Base64().encode("Username:Pwd1".getBytes()));  
    get.setHeader("Accept", "application/json");
    get.setHeader("Authorization", basicAuth);
    get.setHeader("Content-type", "application/json");


    HttpHost target = new HttpHost("Proc1c01.internal.prglobal.com", 8080, "https");

    /*  RequestConfig config = RequestConfig.custom()
                    .setProxy(target)
                    .build();
            get.setConfig(config);*/

    //   HttpResponse response = httpclient.execute(target, get);


    try {
    HttpResponse response = httpclient.execute(get);
    System.out.println(response.getStatusLine().getStatusCode());

    } finally 
    {
    ((Closeable) response).close();
            }
    } finally 
    {
    httpclient.close();
    }
    }

    }

    Error:
    Exception in thread "main" org.apache.http.conn.HttpHostConnectException: Connect to URL failed: Connection refused: connect
        at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:151)
        at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)
        at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:388)
        at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
        at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
        at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
        at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
        at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:117)
        at Peformance.getResponse.main(getResponse.java:44)
    Caused by: java.net.ConnectException: Connection refused: connect
        at java.net.DualStackPlainSocketImpl.connect0(Native Method)
        at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
        at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
        at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
        at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
        at java.net.PlainSocketImpl.connect(Unknown Source)
        at java.net.SocksSocketImpl.connect(Unknown Source)
        at java.net.Socket.connect(Unknown Source)
        at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:337)
        at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:134)
        ... 9 more

0 个答案:

没有答案