Randnum
Randnum

Reputation: 1360

connecting to a URL in Java through a proxy

I'm trying to write a small java program that connects to a twitter search URL (which returns a JSON list of tweets) using the URL connection libary.

My code which is taken from the java tutorials looks like :

        public static void main(String[] args) throws Exception {
        URL oracle = new URL("http://search.twitter.com/search.json?q=hi");
        URLConnection yc = oracle.openConnection();
        BufferedReader in = new BufferedReader(
                                new InputStreamReader(
                                yc.getInputStream()));
        String inputLine;

        while ((inputLine = in.readLine()) != null) 
            System.out.println(inputLine);
        in.close();
    }

but for some reason I keep getting the following Exception:

in thread "main" java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)

I don't know if this is something due to the way I've written the code, and eclipse setting or something to do with my network. I do have a proxy server configured for internet access. As far as I know this is properly configured because I am getting updates and can install new software through eclipse. Do I need to put the proxy information in the URL method somehow or is something else the problem.

Upvotes: 4

Views: 7450

Answers (2)

Carl Smotricz
Carl Smotricz

Reputation: 67820

Unfortunately, a correct proxy setup in Eclipse seems not to help with proxying Java programs started in Eclipse. Similarly, setting the Java system settings to use the systemwide proxy settings doesn't either. Not when you have a proxy that requires authentication, anyway.

As Thomas Johan Eggum said, if you have a "normal," non-authenticating proxy then setting the two JVM variables http.proxyHost and http.proxyPort either in the command line via -D or programmatically (see below) will take care of things.

For an authenticating proxy server, i.e. one that wants to see a user ID and password, many people recommend setting http.proxyUser and http.proxyPassword. That's bad advice, because these don't work. Apparently they are not defined in the Java docs.

Unfortunately, it looks like the way to "do" authentication is to use an Authenticator, programmatically. If you're going to do that, you might as well do the whole thing programmatically, i.e. including host and port. Here's how I got that to work:

   public static void main(String[] args) {
      try {

         System.setProperty("http.proxyHost", "my.proxy.host");
         System.setProperty("http.proxyPort", "8080-or-whatever-proxy-port");
         Authenticator.setDefault(new DummyAuthenticator());

         /* do your main program stuff */             

      } catch (Exception e) {
         e.printStackTrace();
      }
   }

   private static class DummyAuthenticator extends Authenticator {
      public PasswordAuthentication getPasswordAuthentication() {
         return new PasswordAuthentication(
               "my-user-id", "my-password".toCharArray()
               );
      }
   }

Upvotes: 4

Thomas Johan Eggum
Thomas Johan Eggum

Reputation: 915

URL rely on System properties for proxy, try setting proxy like this:

System.setProperty("http.proxyHost", "yourproxyserver");
System.setProperty("http.proxyPort", "portnumber");

Upvotes: 5

Related Questions