1

我正在使用 HttpURLConnection 类从 Eclipse 连接到外部 Web 服务,然后我收到一条错误消息“连接被拒绝”

public class TestConnection {


    static {
        //for localhost testing only
        javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
            new javax.net.ssl.HostnameVerifier(){

                    public boolean verify(String hostname,
                                      javax.net.ssl.SSLSession sslSession) {
                        if (hostname.equals("localhost")) {
                            return true;
                        }
                        return false;
                    }
                });
    }

    public static void main(String[] args)
    {




    HttpURLConnection urlConnection;
    try {
        //https get method with required parameters
    //  XMLHttpRequest s1 = new XMLHttpRequest ();
        urlConnection = (HttpURLConnection) ((new URL("https://google.com").openConnection()));
        urlConnection.setRequestMethod("GET");
        urlConnection.setRequestProperty("User-Agent", "");
        int HTTPS_RESPONSE_CODE = urlConnection.getResponseCode();

        BufferedReader in = new BufferedReader(
                new InputStreamReader(urlConnection.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        String HTTPS_RESULT=response.toString(); 
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    }

}

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 sun.security.ssl.SSLSocketImpl.connect(Unknown Source)
    at sun.security.ssl.BaseSSLSocketImpl.connect(Unknown Source)
    at sun.net.NetworkClient.doConnect(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source)
    at sun.net.www.protocol.https.HttpsClient.New(Unknown Source)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at java.net.HttpURLConnection.getResponseCode(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(Unknown Source)
    at TestConnection.main(TestConnection.java:19)

但是,如果我尝试从浏览器连接到同一个站点,我可以从服务中获得响应。如果有解决办法,你能告诉我吗?

4

2 回答 2

0

默认情况下,HttpURLConnection该类不允许localhost作为主机名。您需要定义一个自定义主机名验证器,它允许localhost. 您可以将此代码放入static您打算使用的类顶部的块中HttpURLConnection

public final class YourClassName {
    static {
        //for localhost testing only
        javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
            new javax.net.ssl.HostnameVerifier(){

                    public boolean verify(String hostname,
                                      javax.net.ssl.SSLSession sslSession) {
                        if (hostname.equals("localhost")) {
                            return true;
                        }
                        return false;
                    }
                });
    }

    // use HttpURLConnection here ...
}
于 2015-08-03T05:15:32.777 回答
0

请找到工作代码,

PROXY_SERVER 设置为有效的代理服务器,我使用的 http 端口是 8080

proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXY_SERVER, PROXY_PORT));

当您建立连接时,将代理作为参数传递。

urlConnection = (HttpURLConnection) ((new URL(URI).openConnection(proxy)));

于 2015-08-04T11:31:18.577 回答