【问题标题】:Connection refused by proxy configuration代理配置拒绝连接
【发布时间】:2015-02-11 10:37:13
【问题描述】:

我正在尝试编写程序以使用提供商的 REST 服务。

我的问题是有一个带有用户/密码身份验证的代理,我无法解决这个问题。

不使用代理也可以,但我需要使用代理。

这是我没有代理配置的代码。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

@SuppressWarnings("deprecation")

public class callAPI {

public callAPI() {
    // TODO Auto-generated constructor stub
}

public static void main(String[] args) throws ClientProtocolException,
        IOException {

    //System.setProperty("java.net.useSystemProxies", "true");
    @SuppressWarnings("resource")
    HttpClient client = new DefaultHttpClient();

    HttpGet request = new HttpGet(
            "URL");

    HttpResponse response = client.execute(request);

    BufferedReader rd = new BufferedReader(new InputStreamReader(response
            .getEntity().getContent()));

    String line = "";

    while ((line = rd.readLine()) != null) {

        System.out.println(line);

    }

}

}

我已阅读所有帖子并尝试在我的代码中执行此操作,但总是遇到相同的错误(连接被拒绝)。

提前致谢!

【问题讨论】:

    标签: java apache proxy


    【解决方案1】:

    取自Apache doco的例子

    HttpHost target = new HttpHost("localhost", 443, "https");
    HttpHost proxy = new HttpHost("127.0.0.1", 8080, "http");
    
    RequestConfig config = RequestConfig.custom()
                .setProxy(proxy)
                .build();
    HttpGet request = new HttpGet("/");
    request.setConfig(config);
    
    CloseableHttpResponse response = httpclient.execute(target, request);
    

    您也可以尝试添加

    Authenticator.setDefault(new Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password.toCharArray());
        }
    });
    

    【讨论】:

    • 我得到“java.net.UnknownHostException”,但 uri 是正确的。如果我从网络浏览器尝试,我可以访问该服务。在该示例中,未定义用户/密码。
    【解决方案2】:

    尝试使用

    System.getProperties().put("http.proxyHost", "ProxyURL");
    System.getProperties().put("http.proxyPort", "ProxyPort");
    System.getProperties().put("http.proxyUser", "UserName");
    System.getProperties().put("http.proxyPassword", "Password");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-16
      • 2018-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多