dcsimg
Problems connecting through proxy server
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Mark_Paterson
Posted On:   Saturday, May 19, 2001 10:01 AM

I am currently trying to connect through a proxy server with the following code, however, it does not seem to work. That is, when the application is run, I am able to get the contents of the URL even though my proxy server is not running. Is there something that I am missing here. Thanks in advance. Mark import java.net.*; import java.io.*; public class Proxy {     public static void main (String args[]) throws Exception {       Properties systemSettings = System.getProperties();       systemSettings.put("http.proxySet", "true");       systemSettings.put("http.proxyHost", "localhost");    More>>

I am currently trying to connect through a proxy server with the following code, however, it does not seem to work. That is, when the application is run, I am able to get the contents of the URL even though my proxy server is not running. Is there something that I am missing here.

Thanks in advance.

Mark


import java.net.*;

import java.io.*;



public class Proxy {

    public static void main (String args[]) throws Exception {

      Properties systemSettings = System.getProperties();

      systemSettings.put("http.proxySet", "true");

      systemSettings.put("http.proxyHost", "localhost");

      systemSettings.put("http.proxyPort", "3128");

      System.setProperties(systemSettings);

      URL url = new URL("http://www.google.com/");

      URLConnection conn = url.openConnection ();

      BufferedReader in = new BufferedReader (new InputStreamReader(conn.getInputStream()));

      String inLine; 

      while ((inLine = in.readLine()) != null) {

          System.out.println(inLine);

      }

  }

}

   <<Less

Re: Problems connecting through proxy server

Posted By:   Tim_Rohaly  
Posted On:   Sunday, May 20, 2001 11:54 AM

Your definition of "it does not seem to work" seems a
little strange. It sounds like it does work,
just not the way you wanted it to work.
You say "I am able to get the contents
of the URL even though my proxy server is not running".
As it turns out this is what Sun's implementation of
URLConnection is supposed to do - it tries your proxy
first then if it can't connect it tries directly. If a direct
connection also fails, then you get an exception.


This fallback position is useful, for example, when you
are trying to reach an address on your local network
(which should typically not pass through a proxy).


If your network setup allows direct connections to
the outside to
bypass the proxy, and relies on the clients to voluntarily
use the proxy, then your network setup is wrong.


Note also that there is no such property as "http.proxySet".
The Networking FAQ has several entries related to
URLConnections through a proxy from which
you can get further information.

About | Sitemap | Contact