简体   繁体   English

Java HttpUrlConnection抛出Connection Refused

[英]Java HttpUrlConnection throws Connection Refused

I know there are several question regarding this topic But I did't find an answer in any of them. 我知道关于这个话题有几个问题但我没有找到任何答案。

I'm trying to open a connection to my local server but I keep getting connection refused. 我正在尝试打开与本地服务器的连接,但我一直拒绝连接。

I have the server running and I tested the connection with the Browser and with a Google App called Postman and it works. 我有服务器运行,我测试了与浏览器的连接和一个名为Postman的谷歌应用程序,它的工作原理。

It's failing when opening the connection as if there where nothing to connect to. 打开连接时失败,好像没有任何东西要连接。 or maybe something is blocking the connection? 或者有什么东西阻止连接? I tested with firewall and antivirus down, no luck. 我测试了防火墙和防病毒软件,没有运气。

testing in Postman the URL returns a User as it should... 在Postman中测试URL返回一个用户应该...

If I replace the url with " http://www.google.com " It Works fine. 如果我用“ http://www.google.com ”替换网址它工作正常。

here is my code: 这是我的代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 *
 * @author Gabriel
 */
public class HttpConnection {

    public HttpConnection() {

    }

    public void makeRequest() throws MalformedURLException, IOException {

        String url = "http://localhost:8000/users/1";

    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    // optional default is GET
    con.setRequestMethod("GET");

    //add request header
    con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36");
        con.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
        con.setRequestProperty("Accept-Encoding", "gzip,deflate,sdch");
        con.setRequestProperty("Accept-Language", "en-US,en;q=0.8,es;q=0.6");
        con.setRequestProperty("Connection", "keep-alive");
        con.setRequestProperty("Host", "localhost:8000");

    int responseCode = con.getResponseCode();
    System.out.println("\nSending 'GET' request to URL : " + url);
    System.out.println("Response Code : " + responseCode);

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

    while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
    }
    in.close();

    //print result
    System.out.println(response.toString());


    }
}

I have similar code that is working, but my request header is a lot simpler. 我有类似的代码,但我的请求标题更简单。 Basically just: 基本上只是:

con.setRequestProperty("User-Agent", "Mozilla/5.0");

If simplifying the header does not help, I would capture the traffic when using your browser with something like fiddler and then making the request look exactly like that. 如果简化标题没有帮助,我会在使用你的浏览器时捕获流量,如fiddler,然后让请求看起来完全像那样。

I will make a wild guess what can be the problem. 我会猜测可能是什么问题。 It is possible a IPv4/IPv6 problem. 可能存在IPv4 / IPv6问题。

If so, here is two possible solutions 如果是这样,这有两种可能的解决方案

  • If the server is only listening on an ipv6 address, change it to listening to ipv4. 如果服务器仅侦听ipv6地址,请将其更改为侦听ipv4。
  • If the server is listening to ipv4, then force Java to use ipv4 with java.net.preferIPv4Stack=true 如果服务器正在侦听ipv4,那么强制Java使用ipv4和java.net.preferIPv4Stack=true

您可以尝试通过在响应头中设置access-control-allow-origin:*属性,在您尝试连接的API上实现CORS。

The code is good and works great. 代码很好,效果很好。 Now the problem must be on the transportation or network part. 现在问题必须出在运输或网络部分。 What I want to mean is you don't request the right server. 我想说的是你没有请求合适的服务器。 If you use 127.0.0.1 instead of localhost I think you won't get a problem. 如果您使用127.0.0.1而不是localhost,我认为您不会遇到问题。 So, my guest will be that you have a problem in /etc/hosts or C:\\Windows\\System32\\drivers\\etc\\hosts . 所以,我的访客将在/etc/hostsC:\\Windows\\System32\\drivers\\etc\\hosts遇到问题。

I advice you to try a simple test: ping the hostname and check in the output if the ip address is good. 我建议你尝试一个简单的测试:ping主机名,如果ip地址好,请检查输出。

I faced exactly the same problem. 我遇到了完全相同的问题。 Use this instead of localhost: 使用此而不是localhost:

http://[::1]:8000/index.php

Well, put http://localhost:8000/users/1 in your web browser and what do you get? 好吧,在你的网络浏览器中输入http://localhost:8000/users/1 ,你得到了什么? A simple Connection Refused error. 一个简单的Connection Refused错误。 It's not you, it's the website. 这不是你,而是网站。 Also, Url returns websites using Protocol Identifiers(http://, https://) , Ending Domains(.com, .edu, .gov) that's also another reason why you get an error. 此外, Url使用Protocol Identifiers(http://, https://)Ending Domains(.com, .edu, .gov)返回网站Ending Domains(.com, .edu, .gov)这也是您收到错误的另一个原因。

You mentioned that you were opening a connection to your "local server" I am assuming that you are doing this on the same computer that you're hosting the server on? 您提到您正在打开与“本地服务器”的连接我假设您在托管服务器的同一台计算机上执行此操作?

  • Try to open the connection to your local server using a different computer. 尝试使用其他计算机打开与本地服务器的连接。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM