0

I am trying to get some data from openweathermap.org via java, but when I run the code I get a 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.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)

Strangely, when I call this link in 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)

ecbrodie
  • 11,246
  • 21
  • 71
  • 120
Burkhard
  • 14,596
  • 22
  • 87
  • 108
  • Have you checked if there is any firewall block? – ADTC Oct 25 '12 at 17:42
  • 2
    I compiled your code and it works fine (well, actually it fetches JSON instead of XML but it's just how the service works when given a `text/xml` or `application/xml` `Accept` header). The problem lies elsewhere. Do you have any firewall runnning? – toniedzwiedz Oct 25 '12 at 17:42
  • 1
    Thanks Tom. What I don't understand is, why I can connect with firefox and not with java. Do they use different ports? I'll check with my sysadmin tomorrow, since I do have control over the firewall. – Burkhard Oct 25 '12 at 17:50
  • 2
    Are you behind a filtering corporate firewall/proxy, such as Cisco AnyConnect? Systems like this do deep packet inspection and can deny connections based on the user-agent string or the program attempting the connection. – Jim Garrison Oct 25 '12 at 17:54
  • 1
    @Burkhard well, I certaintly don't know that either. Firewalls can operate in different layers of the ISO/OSI model, a rule doesn't have to be bound to a port. Some firewalls can block certain applications from accessing the network. – toniedzwiedz Oct 25 '12 at 17:57

2 Answers2

1

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.

Thanks Tom and Jim Garrison for their usefull comments!

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));
}
Burkhard
  • 14,596
  • 22
  • 87
  • 108
-1

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.