Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

connecting to a URL in Java through a proxy

Tags:

java

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.

like image 212
Randnum Avatar asked Jan 19 '23 02:01

Randnum


2 Answers

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

System.setProperty("http.proxyHost", "yourproxyserver");
System.setProperty("http.proxyPort", "portnumber");
like image 84
Thomas Johan Eggum Avatar answered Jan 25 '23 14:01

Thomas Johan Eggum


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()
               );
      }
   }
like image 26
Carl Smotricz Avatar answered Jan 25 '23 14:01

Carl Smotricz