Proxy with optional basic authentication. We have a ServerComm class
that contains server connection info. You should be
able to figure out what it is doing.

---> START
        DefaultHttpClient cli = new DefaultHttpClient();

        HttpGet method = null;
        HttpHost targetHost = null;
        HttpHost proxy = null;

        if (ServerComm.UseProxy )
        {
            if ( ServerComm.UseProxyAuth)
            {
                cli.getCredentialsProvider().setCredentials(
                    new AuthScope(ServerComm.ProxyAddress,
ServerComm.ProxyPort),
                    new
UsernamePasswordCredentials(ServerComm.ProxyUser, ServerComm.ProxyPw)
                    );
            }
            targetHost = new HttpHost(ServerComm.GetServerSite(),
ServerComm.ServerPort, ServerComm.Method);
            proxy = new HttpHost(ServerComm.ProxyAddress, ServerComm.ProxyPort);

            cli.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

            try {
                URI newUri =  new URI(ServerComm.GetServerUrl());
                method = new HttpGet(newUri.getRawPath() + "?" +
newUri.getRawQuery());

            } catch (URISyntaxException ex) {
                System.out.println("URI Encode Error " + ex.toString());
                isCorrect = false;
                return false;
            }

        }
        else
        {
            method = new HttpGet(ServerComm.GetServerUrl());
        }

        HttpResponse resp = null;
        HttpEntity ent = null;

        method.addHeader("User-Agent", ServerComm.MDOUserAgent);

        //execute the method
        String responseBody = null;
        try{
            if (ServerComm.UseProxy )
            {
                resp = cli.execute(targetHost,method);
            }
            else
            {
                resp = cli.execute(method);
            }

           ent = resp.getEntity();
        }
<--- END

On Thu, Jan 24, 2013 at 4:13 PM, Kevin Weslowski <[email protected]> wrote:
> I searched the archives but I can't seem to find it...if you can find it, 
> please send me a link...thanks...
>
> ----- Original Message -----
> From: "David J. Godbey (HQ-LM020)[DIGITAL MANAGEMENT INC.]" 
> <[email protected]>
> To: "HttpClient User Discussion" <[email protected]>
> Sent: Thursday, January 24, 2013 2:37:17 PM GMT -06:00 Central America
> Subject: RE: connect through proxy
>
> Did you see my problem/solution regarding NTLMv2 proxy authentication? You 
> could probably do something like that. That thread appeared around October 11.
>
> -----Original Message-----
> From: Kevin Weslowski [mailto:[email protected]]
> Sent: Thursday, January 24, 2013 3:12 PM
> To: HttpClient User Discussion
> Subject: Re: connect through proxy
>
> thanks, but I couldn't get that to work for me.
>
> I did some research and I have the same problem as found in this post:
>
> http://stackoverflow.com/questions/13308073/httpcomponents-client-proxy-problems
>
> and the suggested solution doesn't work for me...
>
> ----- Original Message -----
> From: "Mark Claassen" <[email protected]>
> To: "HttpClient User Discussion" <[email protected]>
> Sent: Thursday, January 24, 2013 12:45:42 PM GMT -06:00 Central America
> Subject: RE: connect through proxy
>
> I don't know too much about authenticating proxies.  Is it possible that the 
> authentication is done via some magic from your OS?  I know to get my 
> Webstart stuff to work right I needed to also use the java "native" socket 
> factory.  You can try this and see if it helps.
>
> In HttpClient 4.2:
>
> fact = new 
> SSLSocketFactory(HttpsURLConnection.getDefaultSSLSocketFactory(),ver);
> new Scheme(protocol, port , fact);
>
>
> -----Original Message-----
> From: Kevin Weslowski [mailto:[email protected]]
> Sent: Thursday, January 24, 2013 1:23 PM
> To: HttpClient User Discussion
> Subject: Re: connect through proxy
>
> ok, I tried this:
> ========================
> HttpParams params = new BasicHttpParams(); 
> params.setParameter(ConnRoutePNames.DEFAULT_PROXY, new 
> HttpHost("myproxyserver", 8080)); ClientConnectionManager cm = new 
> BasicClientConnectionManager(); HttpClient httpclient = new 
> DefaultHttpClient(cm, params); HttpGet httpGet = new 
> HttpGet("http://www.google.com";); HttpResponse response1 = 
> httpclient.execute(httpGet); System.out.println(response1.getStatusLine());
> ========================
> But the output is always: HTTP/1.1 407 Proxy Authentication Required ( 
> Forefront TMG requires authorization to fulfill the request.
> Access to the Web Proxy filter is denied.  )
>
> I didn't need credentials in my earlier Java program...why not there and why 
> now here? And any ideas how to fix this?
>
> ----- Original Message -----
> From: "Jean-Marc Spaggiari" <[email protected]>
> To: "HttpClient User Discussion" <[email protected]>
> Sent: Thursday, January 24, 2013 11:19:29 AM GMT -06:00 Central America
> Subject: Re: connect through proxy
>
> This piece of code is working for me:
>
> HttpParams params = new BasicHttpParams(); 
> params.setParameter(ConnRoutePNames.DEFAULT_PROXY, new 
> HttpHost(proxyhostname, 80)); client = new DefaultHttpClient(cm, params);
>
> You might want to look at something similar.
>
> JM
>
> 2013/1/24, Kevin Weslowski <[email protected]>:
>> Hi,
>>
>> I have some URLConnection code that connects through a proxy:
>> ============================
>> java.net.URL url = new java.net.URL("http://www.google.com";);
>> java.net.SocketAddress sa = new
>> java.net.InetSocketAddress("myproxyserver",
>> 8080);
>> java.net.Proxy proxy = new java.net.Proxy(java.net.Proxy.Type.HTTP,
>> sa); java.net.HttpURLConnection con = (java.net.HttpURLConnection)
>> url.openConnection(proxy); ============================ and I want to
>> convert that to equivalent httpclient code but I can't seem to find
>> the right way to do that. Can someone point me in the right direction?
>> Thanks...
>>
>> Kevin
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [email protected]
>> For additional commands, e-mail: [email protected]
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to