Thanks Davi! But it didn't work. 

Below the code:

HttpRequestBase httpMethod = null;
HttpResponse response = null;
HttpEntity entity = null;
DefaultHttpClient httpClient = null;
HttpHost proxy = null; 
HttpHost target = null; 
URI uri = null;

StringBuilder resultText = new StringBuilder("");
                
httpClient = new DefaultHttpClient();

proxy = new HttpHost(proxyHost, proxyPort);

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

httpClient.getCredentialsProvider().setCredentials(
        new AuthScope(proxyHost, proxyPort), 
      new UsernamePasswordCredentials(proxyUserName, proxyPassword));

LOG.info("Parsing uri: " + url);
                
uri = new URI(url);
target = new HttpHost(uri.getHost(), uri.getPort(), requestMethod);

                
LOG.info("Connecting to " + uri.getHost() + ":" + uri.getPort() 
        + "," + uri.getRawPath() + "," + uri.getRawQuery() 
      + " via " + requestMethod);               
        
httpMethod = new HttpGet(uri.getRawPath() + ((uri.getRawQuery() == null) ?
"" : ("?" + uri.getRawQuery())));           

response = httpClient.execute(target,httpMethod);

(... and the code to read the output...)

Below the log:

2011/01/25 23:42:55:561 GMT [INFO] HttpPublisher - Parsing uri:
http://www.campacci.com.br:80/
2011/01/25 23:42:55:562 GMT [INFO] HttpPublisher - Connecting to
www.campacci.com.br:80,/,null via GET
2011/01/25 23:42:55:865 GMT [ERROR] HttpPublisher - Exception while making
http request <java.lang.IllegalStateException: Scheme 'get' not
registered.>java.lang.IllegalStateException: Scheme 'get' not registered.
        at
org.apache.http.conn.scheme.SchemeRegistry.getScheme(SchemeRegistry.java:71)
        at
org.apache.http.impl.conn.DefaultHttpRoutePlanner.determineRoute(DefaultHttp
RoutePlanner.java:111)
        at
org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultReq
uestDirector.java:710)
        at
org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDir
ector.java:356)
        at
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.ja
va:820)
        at
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.ja
va:776)
        at
pt.ptinovacao.components.notifier.publisher.impl.http.HttpPublisher.publish(
HttpPublisher.java:101)

Regards,

Rodrigo B. Campacci
[email protected] 


-----Original Message-----
From: David Motes [mailto:[email protected]] 
Sent: terça-feira, 25 de janeiro de 2011 14:42
To: HttpClient User Discussion; [email protected]
Subject: Re: Problem with Proxy Basic Authentication

You don't have a target host.
Try something like this. Note the execute on DefaultHttpClient..
Did a quick cut and paste so you will have to fix it up..

        DefaultHttpClient cli = new DefaultHttpClient();
       HttpGet method = null;
        HttpHost targetHost = null;
        HttpHost proxy = null;

         cli.getCredentialsProvider().setCredentials(
                    new AuthScope(ServerComm.ProxyAddress,
ServerComm.ProxyPort),
                    new
UsernamePasswordCredentials(ServerComm.ProxyUser, ServerComm.ProxyPw)
                    );

            targetHost = new HttpHost(ServerComm.ServerSite,
ServerComm.ServerPort, ServerComm.Method);
            proxy = new HttpHost(ServerComm.ProxyAddress,
ServerComm.ProxyPort);

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

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


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

        HttpResponse resp = null;
        HttpEntity ent = null;



        //execute the method

        String responseBody = null;

        try{

                resp = cli.execute(targetHost,method);


           ent = resp.getEntity();
        }

On Mon, Jan 24, 2011 at 6:50 PM, Rodrigo B. Campacci
<[email protected]> wrote:
> Hi,
>
> I'm trying to make a GET request through a HTTP proxy (squid) with basic
> authentication (user and password).
>
> I found a sample of how to do that using version 3.0, but for version 4.x
I
> can't found this.
>
> I'm using the code below, but it isn't working.
>
>        HttpRequestBase httpmethod = null;
>        HttpResponse response = null;
>        DefaultHttpClient httpclient = null;
>        HttpHost proxy = null;
>
>      StringBuilder resultText = new StringBuilder("");
>
>      httpclient = new DefaultHttpClient();
>
>      proxy = new HttpHost(proxyHost, proxyPort);
>      httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
> proxy);
>
>        httpclient.getCredentialsProvider().setCredentials(
>                                                new AuthScope(proxyHost,
> proxyPort),
>                                                new
> UsernamePasswordCredentials(proxyUserName, proxyPassword));
>
>        httpmethod = new HttpGet(url);
>      response = httpclient.execute(httpmethod);
>
> Below an extract from the log:
> 2011/01/24 00:13:47:054 GMT [DEBUG] DefaultHttpClient - Proxy requested
> authentication
> 2011/01/24 00:13:47:054 GMT [DEBUG] DefaultProxyAuthenticationHandler -
> Authentication schemes in the order of preference: [negotiate, NTLM,
Digest,
> Basic]
> 2011/01/24 00:13:47:054 GMT [DEBUG] DefaultProxyAuthenticationHandler -
> Challenge for negotiate authentication scheme not available
> 2011/01/24 00:13:47:055 GMT [DEBUG] DefaultProxyAuthenticationHandler -
> Challenge for NTLM authentication scheme not available
> 2011/01/24 00:13:47:055 GMT [DEBUG] DefaultProxyAuthenticationHandler -
> Challenge for Digest authentication scheme not available
> 2011/01/24 00:13:47:055 GMT [DEBUG] DefaultProxyAuthenticationHandler -
> Basic authentication scheme selected
> 2011/01/24 00:13:47:064 GMT [DEBUG] DefaultHttpClient - Authorization
> challenge processed
> 2011/01/24 00:13:47:065 GMT [DEBUG] DefaultHttpClient - Authentication
> scope: BASIC '*Enter user and password*'@192.168.160.3:8080
> 2011/01/24 00:13:47:065 GMT [DEBUG] DefaultHttpClient - Found credentials
> 2011/01/24 00:13:47:066 GMT [DEBUG] DefaultClientConnection - Connection
> closed
> 2011/01/24 00:13:47:066 GMT [DEBUG] DefaultClientConnectionOperator -
> Connecting to /192.168.160.3:8080
> 2011/01/24 00:13:47:741 GMT [DEBUG] RequestAddCookies - CookieSpec
selected:
> best-match
> 2011/01/24 00:13:47:747 GMT [DEBUG] DefaultClientConnection - Connection
> shut down
>
> Anyone could help?
>
> Thanks,
>
> Rodrigo B. Campacci
> [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