You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by Ronald Klop <ro...@base.nl> on 2003/02/18 12:56:35 UTC

proxy and howto's and some questions.

Hello,

Are there any howto's about the usage of this package? I can only find 
the API, but some examples are always more clear.

And why aren't the proxy settings not used by the package?
I now made this method to create the HostConfiguration, but it would be 
nice if it's in the package automaticly.

	private static HostConfiguration getHostConfiguration(
			String host, String protocol) {
		String propNameHost = "proxyHost";
		String propNamePort = "proxyPort";

		String proxyHost =
			System.getProperty(protocol+"."+propNameHost);
		Integer proxyPortInt =
			Integer.getInteger(protocol+"."+propNameHost);

		if(proxyHost == null && proxyPortInt == null) {
			// Fall back if the properties do not exist.
			proxyHost = System.getProperty(propNameHost);
			proxyPortInt = Integer.getInteger(propNamePort);
		}

		HostConfiguration config = new HostConfiguration();
		config.setHost(host, -1, protocol); // Use default port.

		if (proxyHost != null && proxyPortInt != null) {
			int proxyPort = proxyPortInt.intValue();
			config.setProxy(proxyHost, proxyPort);
		}

		return config;
	}

And is it needed to make a HostConfiguration by hand?
Can't I just do:
HttpClient hc = new HttpClient();
hc.executeMethod("http://bla1.bla/bla/bla");
hc.executeMethod("http://bla2.bla/bla/bla");
hc.executeMethod("http://bla3.bla/bla/bla");

And that the HostConfiguration is used internaly in HttpClient in stead 
of made by me.
I now have to do the bookkeeping of all the hosts I connect with myself 
and it would be nice if that is all handled automaticly by httpClient.
Just my to cents.

I'm willing to write some code for this, but I don't wont to change the 
API, if other developers don't agree with it.

Greetings,

Ronald.



Re: proxy and howto's and some questions.

Posted by Adrian Sutton <aj...@ajsutton.net>.
Disclaimer: It's 10:30pm, I've been writing documentation for the various
authentication schemes and I normally reply to email in the morning - I hope
this makes sense. :)

> Hello,
>
> Are there any howto's about the usage of this package? I can only find
> the API, but some examples are always more clear.

There are examples in CVS and in the source distribution.  You can view them
online at:
http://cvs.apache.org/viewcvs/jakarta-commons/httpclient/src/examples/

> And why aren't the proxy settings not used by the package?
> I now made this method to create the HostConfiguration, but it would be
> nice if it's in the package automaticly.

The method you provide with fail on various OS's and JVMs.  There is no
requirement that the proxy information be provided in the System Properties
at all let alone what the properties should be called.  The code I'm
currently using which works on the Sun JVM for Windows and Solaris as well
as working on OS X is:

try {
          String noProxy = "";
          if
(System.getProperty("java.vm.vendor").toLowerCase().indexOf("apple") >= 0) {
              proxyHost = System.getProperty("http.proxyHost");
              noProxy = System.getProperty("http.nonProxyHosts", "");
              proxyPort =
Integer.parseInt(System.getProperty("http.proxyPort", "-1"));
              _client.setProxyHost(proxyHost);
              _client.setProxyPort(proxyPort);
          } else {
              String proxy =
System.getProperty("javaplugin.proxy.config.list", "");
              noProxy = System.getProperty("javaplugin.proxy.config.bypass",
"");
              if (proxy.toLowerCase().indexOf("http=") >= 0) {
                  proxy =
proxy.substring(proxy.toLowerCase().indexOf("http=") + "http=".length());
                  if (proxy.indexOf(",") >= 0) {
                      proxy = proxy.substring(0, proxy.indexOf(","));
                  }
                  proxyHost = proxy;
                  proxyPort = 8080;
                  if (proxy.indexOf(":") >= 0) {
                      proxyHost = proxy.substring(0, proxy.indexOf(":"));
                      proxyPort =
Integer.parseInt(proxy.substring(proxy.indexOf(":") + 1));
                  }
                  _client.setProxyHost(proxyHost);
                  _client.setProxyPort(proxyPort);
              }
          }
          StringTokenizer st = new StringTokenizer(noProxy, ",");
          noProxyHosts = new String[st.countTokens()];
          for (int i = 0; i < noProxyHosts.length; i++) {
              noProxyHosts[i] = st.nextToken();
          }
          _client.setNoProxyHosts(noProxyHosts);
      } catch (Exception e) {
          log.error("Failed to detect proxy settings.");
          e.printStackTrace();
      }

Note that I'm using quite an old version of HttpClient (HttpMultiClient
actually) so the actual means of setting the proxy host in HttpClient
probably needs to change now but the basic idea is the same.  I think I also
added support for NoProxyHosts to HttpClient as well, not sure if that's
been added to the main source or not (I can provide the logic I use if
someone has time to update it for the lastest API).

> And is it needed to make a HostConfiguration by hand?
> Can't I just do:
> HttpClient hc = new HttpClient();
> hc.executeMethod("http://bla1.bla/bla/bla");
> hc.executeMethod("http://bla2.bla/bla/bla");
> hc.executeMethod("http://bla3.bla/bla/bla");

You can do something similar:

HttpClient hc = new HttpClient();
GetMethod get = new GetMethod(http://bla1.bla/bla/bla);
get.getHostConfiguration().setProxy("proxy", 8080);
hc.executeMethod(get);

The reason for the extra complexity is that HttpClient has to know what
method you want to execute (GET, POST, PUT, HEAD or perhaps even your own
custom DOOHICKY method).  I would like to see a more central management of
proxy settings but that could be handled client side quite easily anyway.

> And that the HostConfiguration is used internaly in HttpClient in stead
> of made by me.
> I now have to do the bookkeeping of all the hosts I connect with myself
> and it would be nice if that is all handled automaticly by httpClient.
> Just my to cents.

The above code should do pretty much what you want, you still have to manage
the selection of proxy hosts by yourself but generally there will only be
one anyway so it's not that tough.

> Ronald.

Guess we need a tutorial on using HttpClient.  Maybe tomorrow night. :)

Hope that helps, I'll be awake again in another 10 hours or so to answer any
other questions you have.

Adrian Sutton.
Software Engineer, Ephox


Re: proxy and howto's and some questions.

Posted by Michael Becke <be...@u.washington.edu>.
Hello Ronald,

There are some examples in cvs.  You can get to them here:

   http://cvs.apache.org/viewcvs/jakarta-commons/httpclient/src/examples/

I get the impression that HttpClient is not being used much in applets. 
  This explains why there is no built-in support for the browser proxy 
settings.  There has been a little work is creating a HttpUrlConnection 
implementation, but again without proxy support.  If you want to work 
on this please feel free to submit a patch.  Take a look at the 
following for some guidelines:

   http://jakarta.apache.org/site/source.html#Patches

The host configuration is used as a default but can be overridden by 
the methods.  If you want to use host config to set defaults you can 
use the instance provided by HttpClient.

Mike

On Tuesday, February 18, 2003, at 06:56 AM, Ronald Klop wrote:

> Hello,
>
> Are there any howto's about the usage of this package? I can only find 
> the API, but some examples are always more clear.
>
> And why aren't the proxy settings not used by the package?
> I now made this method to create the HostConfiguration, but it would 
> be nice if it's in the package automaticly.
>
> 	private static HostConfiguration getHostConfiguration(
> 			String host, String protocol) {
> 		String propNameHost = "proxyHost";
> 		String propNamePort = "proxyPort";
>
> 		String proxyHost =
> 			System.getProperty(protocol+"."+propNameHost);
> 		Integer proxyPortInt =
> 			Integer.getInteger(protocol+"."+propNameHost);
>
> 		if(proxyHost == null && proxyPortInt == null) {
> 			// Fall back if the properties do not exist.
> 			proxyHost = System.getProperty(propNameHost);
> 			proxyPortInt = Integer.getInteger(propNamePort);
> 		}
>
> 		HostConfiguration config = new HostConfiguration();
> 		config.setHost(host, -1, protocol); // Use default port.
>
> 		if (proxyHost != null && proxyPortInt != null) {
> 			int proxyPort = proxyPortInt.intValue();
> 			config.setProxy(proxyHost, proxyPort);
> 		}
>
> 		return config;
> 	}
>
> And is it needed to make a HostConfiguration by hand?
> Can't I just do:
> HttpClient hc = new HttpClient();
> hc.executeMethod("http://bla1.bla/bla/bla");
> hc.executeMethod("http://bla2.bla/bla/bla");
> hc.executeMethod("http://bla3.bla/bla/bla");
>
> And that the HostConfiguration is used internaly in HttpClient in 
> stead of made by me.
> I now have to do the bookkeeping of all the hosts I connect with 
> myself and it would be nice if that is all handled automaticly by 
> httpClient.
> Just my to cents.
>
> I'm willing to write some code for this, but I don't wont to change 
> the API, if other developers don't agree with it.
>
> Greetings,
>
> Ronald.
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: 
> commons-httpclient-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: 
> commons-httpclient-dev-help@jakarta.apache.org
>