java 为什么我的连接被拒绝?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13073680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:23:53  来源:igfitidea点击:

Why do I get a connection refused?

javarestjerseyconnectionexception

提问by Burkhard

I am trying to get some data from openweathermap.org via java, but when I run the code I get a ConnectionException.

我试图通过 java 从 openweathermap.org 获取一些数据,但是当我运行代码时,我得到了一个 ConnectionException。

My code is:

我的代码是:

public static void openweathermapTest1() {
    String uri = "http://openweathermap.org/data/2.1/find/station?lat=55&lon=37&cnt=10";
    ClientConfig config = new DefaultClientConfig();
    Client client = Client.create(config);
    WebResource service = client.resource(uri);
    String xml = service.accept(MediaType.TEXT_XML).get(String.class);
    System.out.println("Output as XML: " + xml);
}

and the Exception:

和例外:

Exception in thread "main"
com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:151)
at com.sun.jersey.api.client.Client.handle(Client.java:648)     
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:680)   
at com.sun.jersey.api.client.WebResource.access0(WebResource.java:74)
at com.sun.jersey.api.client.WebResource$Builder.get(WebResource.java:507)
at GetPoint.openweathermapTest1(GetPoint.java:110)
at GetPoint.main(GetPoint.java:142)

Strangely, when I call this linkin my web browser, I get the expected data. How can that be? What am I missing here? And how can I fix it? (I tried all three uri and all worked in firefox and not in my programm)

奇怪的是,当我在我的网络浏览器中调用这个链接时,我得到了预期的数据。怎么可能?我在这里错过了什么?我该如何解决?(我尝试了所有三个 uri 并且都在 Firefox 中工作,而不是在我的程序中)

回答by Burkhard

The answer is quite simple: my work computer is behind a proxy and only firefox was using it. With a little proxy magic I was finally able to get the expected result.

答案很简单:我的工作计算机在代理后面,只有 Firefox 使用它。通过一点代理魔法,我终于能够得到预期的结果。

Thanks Tom and Jim Garrison for their usefull comments!

感谢 Tom 和 Jim Garrison 的有用评论!

Edit: I used the following code to use the proxy:

编辑:我使用以下代码来使用代理:

private static void useProxy(String host, int port)
{
    System.setProperty("http.proxySet", "true");
    System.setProperty("http.proxyHost", host);
    System.setProperty("http.proxyPort", String.valueOf(port));
}

回答by SrimathyGanesh

I was facing the same issue. Mine got fixed when i re-checked my JAVA installation. I had both JDK 6 and 7.. this kind of messed up .. so i removed jdk 7 completely and pointed to jdk 6. So command line version , eclipse and server all should point to same jdk.. this resolved the error.

我面临同样的问题。当我重新检查我的 JAVA 安装时,我的得到了修复。我有 JDK 6 和 7 .. 这种混乱.. 所以我完全删除了 jdk 7 并指向 jdk 6。所以命令行版本、eclipse 和服务器都应该指向相同的 jdk.. 这解决了错误。