简体   繁体   English

从Google获取输入流时连接被拒绝

[英]Connection refused while getting inputstream from google

I am writing a method to send a GET request to an endpoint and parse the response text as json. 我正在编写一种将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;
}

However I am getting "Connection refused" error at line InputStream is = con.getInputStream(); 但是我在InputStream is = con.getInputStream();行收到“连接被拒绝”错误InputStream is = con.getInputStream(); . for any url. 对于任何网址。 Even http://www.google.com . 甚至http://www.google.com These urls are accessible from a browser or using curl command on the same machine. 这些URL可以从浏览器访问,也可以在同一台计算机上使用curl命令访问。 What is causing the connection failure using HttpUrlConnection? 是什么导致使用HttpUrlConnection的连接失败?

Below is the stack trace: 下面是堆栈跟踪:

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)

EDIT Based on the answers, looks like the consensus is that this is a firewall issue. 编辑根据答案,似乎共识是这是防火墙问题。

I did set the environment variables http_proxy and https_proxy to the following for my computer https://user:pass@np1prxy801.corp.company.com:80 我确实为我的计算机https:// user:pass@np1prxy801.corp.company.com:80将环境变量http_proxyhttps_proxy设置为以下内容

So I added the following code before the connection: 因此,我在连接之前添加了以下代码:

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());
          }
       }
    );

However, I am still getting connection refused error. 但是,我仍然收到connection refused错误。

Before trying to connect, try 尝试连接之前,请尝试

System.setProperty("http.proxyHost", "your.proxy.server.com"); System.setProperty(“ http.proxyHost”,“ your.proxy.server.com”); System.setProperty("http.proxyPort", "{your proxy port}"); System.setProperty(“ http.proxyPort”,“ {您的代理端口}”);

as an experiment 作为实验

Finally got it to work with the following code based on two other posts, Eclipse Outbound connection blocked : The same url works from web browser and How do I make HttpURLConnection use a proxy? 最后,基于其他两个帖子,它可以与以下代码一起使用, Eclipse出站连接被阻止:从Web浏览器 可以使用 相同的url,以及如何使HttpURLConnection使用代理?

    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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 HttpURLConnection 获取 InputStream 对象时出现 FileNotFoundException - FileNotFoundException while getting the InputStream object from HttpURLConnection android应用程序连接到jboss服务器时,拒绝连接 - getting connection refused while connection android app to jboss server Hadoop从Spring Batch Admin开始作业时收到Connection Refused异常 - Hadoop getting Connection Refused exception while starting job from Spring batch admin 从android中的HttpURLConnection获取InputStream时获取UnknownLengthHttpInputStream - Getting UnknownLengthHttpInputStream while getting InputStream from HttpURLConnection in android 检查套接字连接时从BlueToothSocket的InputStream进行无阻塞读取 - Unblocked read from InputStream of a BlueToothSocket while checking connection of the socket 在Android Studio中获取连接被拒绝(连接被拒绝) - Getting Connection refused (Connection refused) in android studio 从OMElement获取InputStream - Getting InputStream from OMElement 从android httpclient程序访问Servlet导致连接被拒绝? - Accessing Servlet from android httpclient programs getting connection refused? 获取 java.net.ConnectException: Connection refused (Connection refused) while creating object using fake-gcs-server 图像 - Getting java.net.ConnectException: Connection refused (Connection refused) while creating object using fake-gcs-server image 获取“连接超时”而不是“连接被拒绝” - Getting “connection timeout” instead of “connection refused”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM