Java HTTPClient提供连接拒绝异常

时间:2019-02-07 04:59:17

标签: java spring rest java-ee apache-httpclient-4.x

我正在尝试创建一个休息客户端,该客户端发布一个xml字符串并检索xml响应。但是当我使用下面的代码尝试时,我遇到了拒绝连接的问题。另外,当我使用postman addon命中我的模拟URL时在chrome中,我得到了xml响应。请帮助我弄清楚为什么会出现此问题以及如何解决此问题

我的代码

def min_list(mylist=[]):    
    mi_list=[]  
    for each in mylist:  
        mi_list.append(sum(list(map(min,zip(bio_df[each])))))  
    return mi_list  

comb_2 = list(combinations(arr2, 2))  
pair2_count = []  
for each in comb_2:  
    pair2_count.append(list(map(min, zip(bio_df[each[0]], bio_df[each[1]]))))

其中输入字符串是我要发布的xml输入。我得到的错误如下:

错误

public String testMockWS(String inputString) throws KeyManagementException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException{
    //String inputStr =  "";

    String postURL = "https://demo0667044.mockable.io/testXMLRestWS";

    HttpClient client = new DefaultHttpClient();
    HttpPost postRequest = new HttpPost(postURL);

    try{
        StringEntity input = new StringEntity(inputString);
        input.setContentType("text/xml");
        postRequest.setEntity(input);   
        CloseableHttpResponse response = client.execute(postRequest);
        if(response.getStatusLine().getStatusCode() != 201){
            throw new RuntimeException("Failed : HTTP error code : "
                    + response.getStatusLine().getStatusCode());
        }
        BufferedReader br = new BufferedReader(
                new InputStreamReader((response.getEntity().getContent())));

        String output;
        System.out.println("Output from Server .... \n");
        while ((output = br.readLine()) != null) {
            System.out.println(output);
        }

        client.getConnectionManager().shutdown();

    } catch(Exception ex){

        ex.printStackTrace();
    } 

    return null;
}

请帮助我解决此问题并获得响应。我还尝试过接受接受信任策略,认为这可能是SSL问题。但这也没有解决。请帮助我获得响应。

1 个答案:

答案 0 :(得分:0)

这对我有用:

HttpHost proxy = new HttpHost(192.168..., Integer.parseInt(80));
httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);