You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Puneet Lakhina <pu...@gmail.com> on 2007/09/05 20:46:17 UTC

Re: How to set proxy information in httpClient object from browser

On 8/29/07, nitya vyas <ni...@gmail.com> wrote:
>
> Hi there,
> I am using a single HTTPClient object (static) for the desktop application
> with different method objects (get,post,multipartpost) for each call..I
> have also implemented StrictSSLSocketFactory implementation (SSL) for that
> httpClient object. Now i m suppose to get the proxy information from the
> default browser and set the proxy for all requests. I believe it must be
> done for the same static httpClient object for my app but dont know how..


Two things
1. How to get the default proxy settings from the system. IDid some googling
on this, found no way to easily get the windows default internet settings
(similar to what firefox can do on windows when you install and run it).

2. How to set a proxy - I have done this for absolute basic httpclient, dont
know if anything different needs to be done for SSL connections. For this
you can use the HostConfiguration parameter of the executeMethod method.
Something like this
HttpClient httpClient = new HttpClient();
GetMethod getMethod = new GetMethod("http://www.apache.org/");
HostConfiguration hostConfig = new HostConfiguration();
//set proxy host and port
hostConfig.setProxy("my.proxy.address",80);

client.executeMethod(getMethod,hostConfig);
//Dont forget
method.releaseConnection();


can anyone please guide me how this can be achieved? I think setProxy() can
> be done on HttpConnection object and not HTTPClient. So how do i set the
> proxy for my httpClient ???
>
> thanks.
> Nitya
>

Hope it helps.

-- 
Puneet

Re: How to set proxy information in httpClient object from browser

Posted by Roland Weber <os...@dubioso.net>.
Hello Nitya,

> in the implementation they say that you need to write this...
> 
> Protocol stricthttps = new Protocol( "https", new
> StrictSSLProtocolSocketFactory(true), 443);
>      HttpClient client = new HttpClient();
>      client.getHostConfiguration().setHost("hostname", 443, stricthttps);
> 
> Now this Factory implementation stops the man in the middle attack... by
> verifying the hostName... TRUE passed in its constructor..
> 
> But doesnt it mean that it should also call this classes' createSocket()
> method??? because that method has the method verifyHostName() which should
> be called so that hostname is verified???

When SSL connections are tunnelled through a proxy, there is
first a plain HTTP connection to the proxy. That's what you've
made to work now. Subsequently, a tunnel to the target is
established, and the SSL connection with protocol "https"
is layered on top of that. You don't have to verify a hostname
for the connection to the proxy.
> 
> By implementing the above code I see that the createSocket() method of
> StrictSSLProtocolSocketFactory class doesnt get called.. why is that??? or i
> m missing something here????

Have a look at HttpConnection.tunnelCreated, that's where
the SSL connection is layered on top of the tunnel.

If you have specific SSL questions, you should also consider
posting them to the nyc-ssl mailing list:
http://www.juliusdavies.ca/commons-ssl/

hope that helps,
  Roland



---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org


Re: How to set proxy information in httpClient object from browser

Posted by nitya vyas <ni...@gmail.com>.
thank you roland.. yes.. it works with the version 3.1 by registering the
protocol with the ID "http"..

I have one more question regarding the StrictSSLProtocolSocketFactory
implementation..  :)

in the implementation they say that you need to write this...

Protocol stricthttps = new Protocol( "https", new
StrictSSLProtocolSocketFactory(true), 443);
     HttpClient client = new HttpClient();
     client.getHostConfiguration().setHost("hostname", 443, stricthttps);

Now this Factory implementation stops the man in the middle attack... by
verifying the hostName... TRUE passed in its constructor..

But doesnt it mean that it should also call this classes' createSocket()
method??? because that method has the method verifyHostName() which should
be called so that hostname is verified???

By implementing the above code I see that the createSocket() method of
StrictSSLProtocolSocketFactory class doesnt get called.. why is that??? or i
m missing something here????

thanks again...

cheers,
nitya

On 9/19/07, Roland Weber <os...@dubioso.net> wrote:
>
> nitya vyas wrote:
> > i m using 2.0 but the code is same..
>
> No it's not. Upgrade! 2.0 has been unsupported for an eternity.
>
> > in 3.1 also i will have isSecure() and
> > isProxied() true.... because i want https and proxy server both...
>
> Yes.
>
> > So that
> > means that DefaultProtocolSocketFactory will get initialized and not the
> > other one..
>
> No it doesn't. Look at the code fragment I quoted:
>
> >>         if (isSecure() && isProxied()) {
> >>             Protocol defaultprotocol = Protocol.getProtocol("http");
> >>             socketFactory = defaultprotocol.getSocketFactory();
> >>         } else {
> >>             socketFactory = this.protocolInUse.getSocketFactory();
> >>         }
>
> The factory is obtained from a protocol, which is *registered*
> under the name "http". You can register _your_own_ protocol with
> that name, and then _your_ socket factory is used.
>
> Let me know when you have upgraded to 3.1.
> Until then, don't expect me to help you out.
>
> cheers,
>   Roland
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-user-help@jakarta.apache.org
>
>

Re: How to set proxy information in httpClient object from browser

Posted by Roland Weber <os...@dubioso.net>.
nitya vyas wrote:
> i m using 2.0 but the code is same..

No it's not. Upgrade! 2.0 has been unsupported for an eternity.

> in 3.1 also i will have isSecure() and
> isProxied() true.... because i want https and proxy server both...

Yes.

> So that
> means that DefaultProtocolSocketFactory will get initialized and not the
> other one..

No it doesn't. Look at the code fragment I quoted:

>>         if (isSecure() && isProxied()) {
>>             Protocol defaultprotocol = Protocol.getProtocol("http");
>>             socketFactory = defaultprotocol.getSocketFactory();
>>         } else {
>>             socketFactory = this.protocolInUse.getSocketFactory();
>>         }

The factory is obtained from a protocol, which is *registered*
under the name "http". You can register _your_own_ protocol with
that name, and then _your_ socket factory is used.

Let me know when you have upgraded to 3.1.
Until then, don't expect me to help you out.

cheers,
  Roland


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org


Re: How to set proxy information in httpClient object from browser

Posted by nitya vyas <ni...@gmail.com>.
i m using 2.0 but the code is same.. in 3.1 also i will have isSecure() and
isProxied() true.... because i want https and proxy server both... So that
means that DefaultProtocolSocketFactory will get initialized and not the
other one.. thAt is protocolInUse.getsocketFactory() which will be
MySocketFactory() in my case as i m registering the protocol by it....

So it wont be called and my impl of createSocket will be useless... :(  (or
i have to change the HTTPConnection class ..)

On 9/19/07, Roland Weber <os...@dubioso.net> wrote:
>
> nitya vyas wrote:
> > The problem I find now is that this createSocket method does not get
> called
> > from the HTTPConnection class... the reason being this line of
> code.....(i
> > dont wanna change the HTTPConnection if theres another way.. )
> >
> > final ProtocolSocketFactory socketFactory = (isSecure() && isProxied()?
> new
> > DefaultProtocolSocketFactory() : protocolInUse.getSocketFactory());
> >
>
> What version of HttpClient are you using?
> This is what I find in the current code (3.1 final):
>
>         if (isSecure() && isProxied()) {
>             Protocol defaultprotocol = Protocol.getProtocol("http");
>             socketFactory = defaultprotocol.getSocketFactory();
>         } else {
>             socketFactory = this.protocolInUse.getSocketFactory();
>         }
>
> So you can simply register your own protocol "http"
> that calls an instance of your socket factory.
>
> cheers,
>   Roland
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-user-help@jakarta.apache.org
>
>

Re: How to set proxy information in httpClient object from browser

Posted by Roland Weber <os...@dubioso.net>.
nitya vyas wrote:
> The problem I find now is that this createSocket method does not get called
> from the HTTPConnection class... the reason being this line of code.....(i
> dont wanna change the HTTPConnection if theres another way.. )
> 
> final ProtocolSocketFactory socketFactory = (isSecure() && isProxied()? new
> DefaultProtocolSocketFactory() : protocolInUse.getSocketFactory());
> 

What version of HttpClient are you using?
This is what I find in the current code (3.1 final):

        if (isSecure() && isProxied()) {
            Protocol defaultprotocol = Protocol.getProtocol("http");
            socketFactory = defaultprotocol.getSocketFactory();
        } else {
            socketFactory = this.protocolInUse.getSocketFactory();
        }

So you can simply register your own protocol "http"
that calls an instance of your socket factory.

cheers,
  Roland


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org


Re: How to set proxy information in httpClient object from browser

Posted by nitya vyas <ni...@gmail.com>.
Hi guys....

got a problem again!!! :( :( i m told that as far as possible dont change
the httpClient jar so I will have to find a way to do as described in
previous mail without changing HTTPConnection.java class....

I think if  i do this....
Protocol stricthttps = new Protocol(SystemConstants.protocolHttps, new
MySocketFactory(), SystemConstants.socketNumber);

and this...

httpClient.getHostConfiguration().setHost(SystemConstants.connectionHostName,
SystemConstants.socketNumber, stricthttps);
httpClient.getHostConfiguration().setProxy(proxyHost, proxyPort);

MySocketFactory class does this... (because i also want StrictSSL
implementation.. )

MySocketFactory  extends StrictSSLProtocolSocketFactory implements
ProtocolSocketFactory{
     MySocketFactory() throws Exception {
          super(true);
    }

    public Socket createSocket(host,port){
       //my code for creating socket....
   }
}

The problem I find now is that this createSocket method does not get called
from the HTTPConnection class... the reason being this line of code.....(i
dont wanna change the HTTPConnection if theres another way.. )

final ProtocolSocketFactory socketFactory = (isSecure() && isProxied()? new
DefaultProtocolSocketFactory() : protocolInUse.getSocketFactory());

here isSecure() and isProxied() both are true because i want both of them..
but the problem is that DefaultProtocolSocketFactory gets initiated and not
protocolInUse.getSocketFactory() so mysocketfactory class never gets
called...

is there a problem in my implementation or is there any other way to do
this??

thanks..


On 9/18/07, nitya vyas <ni...@gmail.com> wrote:
>
> I got it.. I had to call the bind and connect mathod also from my
> implementation..
>
> public Socket createSocket(String host, int port) throws IOException,
> UnknownHostException {
>         Socket s = new Socket(Proxy.NO_PROXY );
>         InetSocketAddress addr = new InetSocketAddress(host, port);
>         s.bind(new InetSocketAddress(0));
>         s.connect(addr);
>         return s;
>     }
>
> Looks to be wroking fine.. Now it goes to the PlainSocketImpl class rather
> than SocksSocketImpl.. i hope there wont be any implications of the
> commons-httpclient.jar change to other things as this method will be
> called now for every server call..
>
> cheers,
> Nitya
>
> On 9/17/07, nitya vyas < nitya.vyas@gmail.com> wrote:
> >
> > thanks Roland..
> >
> > this looks like the solution as i also saw that It was checking proxy ==
> > NO_PROXY somewhere and it didnt get it in my implementation so it threw
> > IOException.
> >
> > I still have a question regarding the implementation of the
> > socketfactory. if i use new Socket( Proxy.NO_PROXY) then i m getting
> > java.net.SocketException: Socket is not connected exception.. can u pls
> > tell me what is the reason? i guess the host and port that we get must be
> > set in the Socket class just like it happens right now by new
> > Socket(host,port) from HTTPConnection class.. but doesnt happen when i do
> > new socket( Proxy.NO_PROXY)...
> >
> > i called my implementation createSocket() method from the HTTPConnection
> > class..this class is called when i do httpClient.execute(method)...
> >
> > please help..
> >
> > thanks
> >
> >
> > On 9/14/07, Roland Weber <os...@dubioso.net> wrote:
> > >
> > > nitya vyas wrote:
> > > > I found out that the problem lies in only one line ..
> > > >
> > > > System.setProperty("java.net.useSystemProxies","true");
> > > >
> > > > if i dont do this, the server will respond. or if i immediately
> > > write
> > > > System.setProperty("java.net.useSystemProxies","false"); after that
> > > line
> > > > only nothng in between then only it works and server responds..
> > >
> > > Implement a ProtocolSocketFactory [1].
> > > Use the Socket(Proxy) constructor [2]
> > > with argument Proxy.NO_PROXY [3], then
> > > connect. Mark Claassen did that before [4].
> > >
> > > hope that helps,
> > >   Roland
> > >
> > > [1]
> > >
> > > http://jakarta.apache.org/httpcomponents/httpclient-3.x/apidocs/org/apache/commons/httpclient/protocol/ProtocolSocketFactory.html
> > > [2]
> > >
> > > http://java.sun.com/j2se/1.5.0/docs/api/java/net/Socket.html#Socket(java.net.Proxy)<http://java.sun.com/j2se/1.5.0/docs/api/java/net/Socket.html#Socket%28java.net.Proxy%29>
> > > [3] http://java.sun.com/j2se/1.5.0/docs/api/java/net/Proxy.html#NO_PROXY
> > >
> > > [4]
> > > http://mail-archives.apache.org/mod_mbox/jakarta-httpclient-user/200701.mbox/%3C012a01c73f2d$d9b897a0$19c909c0@K9%3E
> > >
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
> > > For additional commands, e-mail:
> > > httpclient-user-help@jakarta.apache.org
> > >
> > >
> >
>

Re: How to set proxy information in httpClient object from browser

Posted by nitya vyas <ni...@gmail.com>.
I got it.. I had to call the bind and connect mathod also from my
implementation..

public Socket createSocket(String host, int port) throws IOException,
UnknownHostException {
        Socket s = new Socket(Proxy.NO_PROXY);
        InetSocketAddress addr = new InetSocketAddress(host, port);
        s.bind(new InetSocketAddress(0));
        s.connect(addr);
        return s;
    }

Looks to be wroking fine.. Now it goes to the PlainSocketImpl class rather
than SocksSocketImpl.. i hope there wont be any implications of the
commons-httpclient.jar change to other things as this method will be called
now for every server call..

cheers,
Nitya

On 9/17/07, nitya vyas <ni...@gmail.com> wrote:
>
> thanks Roland..
>
> this looks like the solution as i also saw that It was checking proxy ==
> NO_PROXY somewhere and it didnt get it in my implementation so it threw
> IOException.
>
> I still have a question regarding the implementation of the socketfactory.
> if i use new Socket( Proxy.NO_PROXY) then i m getting
> java.net.SocketException: Socket is not connected exception.. can u pls
> tell me what is the reason? i guess the host and port that we get must be
> set in the Socket class just like it happens right now by new
> Socket(host,port) from HTTPConnection class.. but doesnt happen when i do
> new socket( Proxy.NO_PROXY)...
>
> i called my implementation createSocket() method from the HTTPConnection
> class..this class is called when i do httpClient.execute(method)...
>
> please help..
>
> thanks
>
>
> On 9/14/07, Roland Weber <os...@dubioso.net> wrote:
> >
> > nitya vyas wrote:
> > > I found out that the problem lies in only one line ..
> > >
> > > System.setProperty("java.net.useSystemProxies","true");
> > >
> > > if i dont do this, the server will respond. or if i immediately write
> > > System.setProperty("java.net.useSystemProxies","false"); after that
> > line
> > > only nothng in between then only it works and server responds..
> >
> > Implement a ProtocolSocketFactory [1].
> > Use the Socket(Proxy) constructor [2]
> > with argument Proxy.NO_PROXY [3], then
> > connect. Mark Claassen did that before [4].
> >
> > hope that helps,
> >   Roland
> >
> > [1]
> >
> > http://jakarta.apache.org/httpcomponents/httpclient-3.x/apidocs/org/apache/commons/httpclient/protocol/ProtocolSocketFactory.html
> > [2]
> >
> > http://java.sun.com/j2se/1.5.0/docs/api/java/net/Socket.html#Socket(java.net.Proxy)<http://java.sun.com/j2se/1.5.0/docs/api/java/net/Socket.html#Socket%28java.net.Proxy%29>
> > [3] http://java.sun.com/j2se/1.5.0/docs/api/java/net/Proxy.html#NO_PROXY
> >
> > [4]
> > http://mail-archives.apache.org/mod_mbox/jakarta-httpclient-user/200701.mbox/%3C012a01c73f2d$d9b897a0$19c909c0@K9%3E
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: httpclient-user-help@jakarta.apache.org
> >
> >
>

Re: How to set proxy information in httpClient object from browser

Posted by nitya vyas <ni...@gmail.com>.
thanks Roland..

this looks like the solution as i also saw that It was checking proxy ==
NO_PROXY somewhere and it didnt get it in my implementation so it threw
IOException.

I still have a question regarding the implementation of the socketfactory.
if i use new Socket( Proxy.NO_PROXY) then i m getting
java.net.SocketException: Socket is not connected exception.. can u pls tell
me what is the reason? i guess the host and port that we get must be set in
the Socket class just like it happens right now by new Socket(host,port)
from HTTPConnection class.. but doesnt happen when i do new socket(
Proxy.NO_PROXY)...

i called my implementation createSocket() method from the HTTPConnection
class..this class is called when i do httpClient.execute(method)...

please help..

thanks


On 9/14/07, Roland Weber <os...@dubioso.net> wrote:
>
> nitya vyas wrote:
> > I found out that the problem lies in only one line ..
> >
> > System.setProperty("java.net.useSystemProxies","true");
> >
> > if i dont do this, the server will respond. or if i immediately write
> > System.setProperty("java.net.useSystemProxies","false"); after that line
> > only nothng in between then only it works and server responds..
>
> Implement a ProtocolSocketFactory [1].
> Use the Socket(Proxy) constructor [2]
> with argument Proxy.NO_PROXY [3], then
> connect. Mark Claassen did that before [4].
>
> hope that helps,
>   Roland
>
> [1]
>
> http://jakarta.apache.org/httpcomponents/httpclient-3.x/apidocs/org/apache/commons/httpclient/protocol/ProtocolSocketFactory.html
> [2]
>
> http://java.sun.com/j2se/1.5.0/docs/api/java/net/Socket.html#Socket(java.net.Proxy)<http://java.sun.com/j2se/1.5.0/docs/api/java/net/Socket.html#Socket%28java.net.Proxy%29>
> [3] http://java.sun.com/j2se/1.5.0/docs/api/java/net/Proxy.html#NO_PROXY
> [4]
> http://mail-archives.apache.org/mod_mbox/jakarta-httpclient-user/200701.mbox/%3C012a01c73f2d$d9b897a0$19c909c0@K9%3E
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-user-help@jakarta.apache.org
>
>

Re: How to set proxy information in httpClient object from browser

Posted by Roland Weber <os...@dubioso.net>.
nitya vyas wrote:
> I found out that the problem lies in only one line ..
> 
> System.setProperty("java.net.useSystemProxies","true");
> 
> if i dont do this, the server will respond. or if i immediately write
> System.setProperty("java.net.useSystemProxies","false"); after that line
> only nothng in between then only it works and server responds..

Implement a ProtocolSocketFactory [1].
Use the Socket(Proxy) constructor [2]
with argument Proxy.NO_PROXY [3], then
connect. Mark Claassen did that before [4].

hope that helps,
  Roland

[1]
http://jakarta.apache.org/httpcomponents/httpclient-3.x/apidocs/org/apache/commons/httpclient/protocol/ProtocolSocketFactory.html
[2]
http://java.sun.com/j2se/1.5.0/docs/api/java/net/Socket.html#Socket(java.net.Proxy)
[3] http://java.sun.com/j2se/1.5.0/docs/api/java/net/Proxy.html#NO_PROXY
[4]
http://mail-archives.apache.org/mod_mbox/jakarta-httpclient-user/200701.mbox/%3C012a01c73f2d$d9b897a0$19c909c0@K9%3E

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org


Re: How to set proxy information in httpClient object from browser

Posted by nitya vyas <ni...@gmail.com>.
thats not the issue here... its about getting the values of proxyHost and
proxyPort when the client is running the desktop application.

I do get the host and port fromthe browser by ProxySelector class.. as i
mentioned above. but the problem is that it just doesnt connect to the
server. connection reset by peer......

I found out that the problem lies in only one line ..

System.setProperty("java.net.useSystemProxies","true");

if i dont do this, the server will respond. or if i immediately write
System.setProperty("java.net.useSystemProxies","false"); after that line
only nothng in between then only it works and server responds..

THis is really creepy.. and i dont know the solution of this problem. but i
need to set this property to true if i want to get the proxy settings of the
browser... :(:(:(

too bad... can anyone pls help???


On 9/12/07, Tobias Meier <To...@nepatec.de> wrote:
>
> Hi!
>
> I set the proxy in the HTTPClient in the following way:
>
>   SimpleHttpConnectionManager connectionManager = new
> SimpleHttpConnectionManager();
>   HttpClient client = new HttpClient(this.connectionManager);
>
>   HostConfiguration config =
> this.client.getHostConfiguration();
>   //use Proxy
>   this.config.setProxy(proxyHost, proxyPort);
>
>   //Proxy Authentication
>   Credentials defaultcreds = new
> UsernamePasswordCredentials(proxyUser, proxyPassword);
>   AuthScope proxyScope = new AuthScope( proxyHost, proxyPort
> );
>   this.client.getState().setProxyCredentials(proxyScope,
> defaultcreds);
>
> regards,
> ToM
>
>
> Am Mi 12.09.2007 13:55 schrieb nitya vyas <ni...@gmail.com>:
>
> > I get the proxy settings with following code..... userful one...
> >
> > System.setProperty("java.net.useSystemProxies","true");
> > List l = ProxySelector.getDefault().select(new URI("
> > https://www.yahoo.com"));
> > for (Iterator iter = l.iterator(); iter.hasNext(); ){
> > Proxy proxy1 = (Proxy) iter.next();
> > System.out.println("proxy type : " + proxy1.type());
> > InetSocketAddress addr = (InetSocketAddress)
> > proxy1.address();
> > if(addr == null){
> > System.out.println("No Proxy");
> > }else{
> > String proxyHost = addr.getHostName();
> > int proxyPort = addr.getPort();
> > System.out.println("proxy hostname : " +
> > addr.getHostName());
> > System.out.println("proxy port : " + addr.getPort
> > ());
> > }
> > }
> >
> > When i run this code i get the proxy settings of the IE.. eventhough
> > my
> > default browser is Firefox i got the settings from IE. i checked the
> > same by
> > removing from IE and didnt get anything in that case..
> >
> > Now when i use the same httpClient object with setting proxy... and
> > try to
> > execute the method... httpClient.executeMethod(xyz) it just stops
> > there..
> > and then after a long wait it gives java.net.SocketException:
> > Connection
> > reset ......
> >
> > i dont know wat is wrong.. can anyone throw some light on this?????
> >
> > thanks
> >
> > On 9/6/07, Puneet Lakhina <pu...@gmail.com> wrote:
> > >
> > > On 8/29/07, nitya vyas <ni...@gmail.com> wrote:
> > > >
> > > > Hi there,
> > > > I am using a single HTTPClient object (static) for the desktop
> > > application
> > > > with different method objects (get,post,multipartpost) for each
> > > > call..I
> > > > have also implemented StrictSSLSocketFactory implementation (SSL)
> > > > for
> > > that
> > > > httpClient object. Now i m suppose to get the proxy information
> > > > from the
> > > > default browser and set the proxy for all requests. I believe it
> > > > must be
> > > > done for the same static httpClient object for my app but dont
> > > > know
> > > how..
> > >
> > >
> > > Two things
> > > 1. How to get the default proxy settings from the system. IDid some
> > > googling
> > > on this, found no way to easily get the windows default internet
> > > settings
> > > (similar to what firefox can do on windows when you install and run
> > > it).
> > >
> > > 2. How to set a proxy - I have done this for absolute basic
> > > httpclient,
> > > dont
> > > know if anything different needs to be done for SSL connections. For
> > > this
> > > you can use the HostConfiguration parameter of the executeMethod
> > > method.
> > > Something like this
> > > HttpClient httpClient = new HttpClient();
> > > GetMethod getMethod = new GetMethod("http://www.apache.org/");
> > > HostConfiguration hostConfig = new HostConfiguration();
> > > //set proxy host and port
> > > hostConfig.setProxy("my.proxy.address",80);
> > >
> > > client.executeMethod(getMethod,hostConfig);
> > > //Dont forget
> > > method.releaseConnection();
> > >
> > >
> > > can anyone please guide me how this can be achieved? I think
> > > setProxy()
> > > can
> > > > be done on HttpConnection object and not HTTPClient. So how do i
> > > > set the
> > > > proxy for my httpClient ???
> > > >
> > > > thanks.
> > > > Nitya
> > > >
> > >
> > > Hope it helps.
> > >
> > > --
> > > Puneet
> > >
>
>
> VieleGrüße,
> TobiasMeier
> __________________________________________________
> MSc,Dipl.-Inf.TobiasMeier
> Softwareentwickler
> nepatecGmbH&Co.KG
> Hindenburgstr.37.30175Hannover
> Fon+49(0)511935946.51,Fax+49(0)511935946.57
> Mailto:tobias.meier@nepatec.de
> http://www.nepatec.de
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-user-help@jakarta.apache.org
>
>

Re: How to set proxy information in httpClient object from browser

Posted by Tobias Meier <To...@nepatec.de>.
Hi!

I set the proxy in the HTTPClient in the following way:

            SimpleHttpConnectionManager connectionManager = new
SimpleHttpConnectionManager();
            HttpClient client = new HttpClient(this.connectionManager);

            HostConfiguration config =
this.client.getHostConfiguration();
            //use Proxy
            this.config.setProxy(proxyHost, proxyPort);   
               
            //Proxy Authentication
            Credentials defaultcreds = new
UsernamePasswordCredentials(proxyUser, proxyPassword);
            AuthScope proxyScope = new AuthScope( proxyHost, proxyPort
);
            this.client.getState().setProxyCredentials(proxyScope,
defaultcreds);

regards,
ToM


Am Mi 12.09.2007 13:55 schrieb nitya vyas <ni...@gmail.com>:

> I get the proxy settings with following code..... userful one...
>
> System.setProperty("java.net.useSystemProxies","true");
> List l = ProxySelector.getDefault().select(new URI("
> https://www.yahoo.com"));
> for (Iterator iter = l.iterator(); iter.hasNext(); ){
> Proxy proxy1 = (Proxy) iter.next();
> System.out.println("proxy type : " + proxy1.type());
> InetSocketAddress addr = (InetSocketAddress)
> proxy1.address();
> if(addr == null){
> System.out.println("No Proxy");
> }else{
> String proxyHost = addr.getHostName();
> int proxyPort = addr.getPort();
> System.out.println("proxy hostname : " +
> addr.getHostName());
> System.out.println("proxy port : " + addr.getPort
> ());
> }
> }
>
> When i run this code i get the proxy settings of the IE.. eventhough
> my
> default browser is Firefox i got the settings from IE. i checked the
> same by
> removing from IE and didnt get anything in that case..
>
> Now when i use the same httpClient object with setting proxy... and
> try to
> execute the method... httpClient.executeMethod(xyz) it just stops
> there..
> and then after a long wait it gives java.net.SocketException:
> Connection
> reset ......
>
> i dont know wat is wrong.. can anyone throw some light on this?????
>
> thanks
>
> On 9/6/07, Puneet Lakhina <pu...@gmail.com> wrote:
> >
> > On 8/29/07, nitya vyas <ni...@gmail.com> wrote:
> > >
> > > Hi there,
> > > I am using a single HTTPClient object (static) for the desktop
> > application
> > > with different method objects (get,post,multipartpost) for each
> > > call..I
> > > have also implemented StrictSSLSocketFactory implementation (SSL)
> > > for
> > that
> > > httpClient object. Now i m suppose to get the proxy information
> > > from the
> > > default browser and set the proxy for all requests. I believe it
> > > must be
> > > done for the same static httpClient object for my app but dont
> > > know
> > how..
> >
> >
> > Two things
> > 1. How to get the default proxy settings from the system. IDid some
> > googling
> > on this, found no way to easily get the windows default internet
> > settings
> > (similar to what firefox can do on windows when you install and run
> > it).
> >
> > 2. How to set a proxy - I have done this for absolute basic
> > httpclient,
> > dont
> > know if anything different needs to be done for SSL connections. For
> > this
> > you can use the HostConfiguration parameter of the executeMethod
> > method.
> > Something like this
> > HttpClient httpClient = new HttpClient();
> > GetMethod getMethod = new GetMethod("http://www.apache.org/");
> > HostConfiguration hostConfig = new HostConfiguration();
> > //set proxy host and port
> > hostConfig.setProxy("my.proxy.address",80);
> >
> > client.executeMethod(getMethod,hostConfig);
> > //Dont forget
> > method.releaseConnection();
> >
> >
> > can anyone please guide me how this can be achieved? I think
> > setProxy()
> > can
> > > be done on HttpConnection object and not HTTPClient. So how do i
> > > set the
> > > proxy for my httpClient ???
> > >
> > > thanks.
> > > Nitya
> > >
> >
> > Hope it helps.
> >
> > --
> > Puneet
> >


Viele Grüße,
Tobias Meier
__________________________________________________
MSc, Dipl.-Inf. Tobias Meier
Softwareentwickler
nepatec GmbH & Co. KG
Hindenburgstr. 37 . 30175 Hannover
Fon +49(0)511 935 946.51, Fax +49(0)511 935 946.57
Mailto: tobias.meier@nepatec.de
http://www.nepatec.de


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org


Re: How to set proxy information in httpClient object from browser

Posted by nitya vyas <ni...@gmail.com>.
I get the proxy settings with following code..... userful one...

          System.setProperty("java.net.useSystemProxies","true");
          List l = ProxySelector.getDefault().select(new URI("
https://www.yahoo.com"));
                for (Iterator iter = l.iterator(); iter.hasNext(); ){
                    Proxy proxy1 = (Proxy) iter.next();
                    System.out.println("proxy type : " + proxy1.type());
                    InetSocketAddress addr = (InetSocketAddress)
proxy1.address();
                    if(addr == null){
                        System.out.println("No Proxy");
                    }else{
                        String proxyHost = addr.getHostName();
                        int proxyPort = addr.getPort();
                        System.out.println("proxy hostname : " +
addr.getHostName());
                        System.out.println("proxy port : " + addr.getPort
());
                    }
                }

When i run this code i get the proxy settings of the IE.. eventhough my
default browser is Firefox i got the settings from IE. i checked the same by
removing from IE and didnt get anything in that case..

Now when i use the same httpClient object with setting proxy... and try to
execute the method... httpClient.executeMethod(xyz) it just stops there..
and then after a long wait it gives java.net.SocketException: Connection
reset ......

i dont know wat is wrong.. can anyone throw some light on this?????

thanks

On 9/6/07, Puneet Lakhina <pu...@gmail.com> wrote:
>
> On 8/29/07, nitya vyas <ni...@gmail.com> wrote:
> >
> > Hi there,
> > I am using a single HTTPClient object (static) for the desktop
> application
> > with different method objects (get,post,multipartpost) for each call..I
> > have also implemented StrictSSLSocketFactory implementation (SSL) for
> that
> > httpClient object. Now i m suppose to get the proxy information from the
> > default browser and set the proxy for all requests. I believe it must be
> > done for the same static httpClient object for my app but dont know
> how..
>
>
> Two things
> 1. How to get the default proxy settings from the system. IDid some
> googling
> on this, found no way to easily get the windows default internet settings
> (similar to what firefox can do on windows when you install and run it).
>
> 2. How to set a proxy - I have done this for absolute basic httpclient,
> dont
> know if anything different needs to be done for SSL connections. For this
> you can use the HostConfiguration parameter of the executeMethod method.
> Something like this
> HttpClient httpClient = new HttpClient();
> GetMethod getMethod = new GetMethod("http://www.apache.org/");
> HostConfiguration hostConfig = new HostConfiguration();
> //set proxy host and port
> hostConfig.setProxy("my.proxy.address",80);
>
> client.executeMethod(getMethod,hostConfig);
> //Dont forget
> method.releaseConnection();
>
>
> can anyone please guide me how this can be achieved? I think setProxy()
> can
> > be done on HttpConnection object and not HTTPClient. So how do i set the
> > proxy for my httpClient ???
> >
> > thanks.
> > Nitya
> >
>
> Hope it helps.
>
> --
> Puneet
>