代理配置拒绝连接

时间:2015-02-11 10:37:13

标签: java apache proxy

我正在尝试编写程序以使用提供程序的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);

    }

}

}

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

提前致谢!

2 个答案:

答案 0 :(得分:0)

取自示例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());
    }
});

答案 1 :(得分:0)

尝试使用

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