为什么我拒绝连接



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

我的代码是:

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

和例外:

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.access$200(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)

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

答案很简单:我的工作计算机是代理后面的,只有Firefox才使用它。有了一点代理魔术,我终于能够获得预期的结果。

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

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

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

我正面临同一问题。当我重新检查我的Java安装时,我的矿井就修复了。我既有JDK 6和7。

相关内容

最新更新