从谷歌获取输入流时拒绝连接

时间:2017-02-21 20:28:41

标签: java httprequest inputstream httpurlconnection

我正在编写一个方法来向端点发送GET请求并将响应文本解析为json。

public static KafkaConnection getKafkaConnection(String sUrl) throws IOException {
    URL url=new URL(sUrl);
    HttpURLConnection con=(HttpURLConnection)url.openConnection();
    con.setRequestMethod("GET");
    InputStream is = con.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder builder = new StringBuilder();
    for (String line = null; (line = reader.readLine()) != null;) {
        builder.append(line).append("\n");
    }
    String jsonText = builder.toString();
    JSONObject json = new JSONObject(jsonText);
    KafkaConnection kc = new KafkaConnection();
    JSONArray array = json.getJSONArray("address");
    List<String> list = new ArrayList<String>();
    for (int i = 0; i < array.length(); i++) {
        list.add(array.getString(i));
    }
    kc.setAddresses(list);
    kc.setZookeeper(json.getString("zookeeper"));
    return kc;
}

但是我得到了#34;连接被拒绝&#34;第InputStream is = con.getInputStream();行的错误。对于任何网址。甚至http://www.google.com。可以从浏览器访问这些URL,也可以在同一台计算机上使用curl命令访问这些URL。使用HttpUrlConnection导致连接失败的原因是什么?

下面是堆栈跟踪:

Exception in thread "main" java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at java.net.Socket.connect(Socket.java:538)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:211)
    at sun.net.www.http.HttpClient.New(HttpClient.java:308)
    at sun.net.www.http.HttpClient.New(HttpClient.java:326)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1202)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1138)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1032)
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:966)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1546)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
    at com.lgc.dist.plat.test.kafka.KafkaTestUtils.getKafkaConnection(KafkaTestUtils.java:30)
    at com.lgc.dist.plat.test.kafka.KafkaTestUtils.main(KafkaTestUtils.java:54)

修改 基于答案,看起来共识是这是一个防火墙问题。

我确实为我的计算机将环境变量http_proxyhttps_proxy设置为以下内容 https://user:pass@np1prxy801.corp.company.com:80

所以我在连接之前添加了以下代码:

System.setProperty("http.proxyHost", "https://np1prxy801.corp.company.com"); 
    System.setProperty("http.proxyPort", "80");
    String user = "user";
    String password = "pass";
    Authenticator.setDefault(
       new Authenticator() {
          @Override
          public PasswordAuthentication getPasswordAuthentication() {
             return new PasswordAuthentication(
                   user, password.toCharArray());
          }
       }
    );

但是,我仍然遇到connection refused错误。

2 个答案:

答案 0 :(得分:1)

在尝试连接之前,请尝试

System.setProperty(&#34; http.proxyHost&#34;,&#34; your.proxy.server.com&#34;); System.setProperty(&#34; http.proxyPort&#34;,&#34; {your proxy port}&#34;);

作为实验

答案 1 :(得分:0)

最后根据其他两篇帖子(Eclipse Outbound connection blocked : The same url works from web browser How do I make HttpURLConnection use a proxy?

使用以下代码
    Proxy proxy = new Proxy(Proxy.Type.HTTP, 
            new InetSocketAddress("np1prxy801.corp.abc.com", 80));
    URL url=new URL(sUrl);
    HttpURLConnection con = (HttpURLConnection)url.openConnection(proxy);

    String user = "user";
    String password = "password";
    Authenticator.setDefault(
       new Authenticator() {
          @Override
          public PasswordAuthentication getPasswordAuthentication() {
             return new PasswordAuthentication(
                   user, password.toCharArray());
          }
       }
    );

    con.setRequestMethod("GET");
    InputStream is = con.getInputStream();