BasicAuthentication using HttpClient 4.0

View: New views
9 Messages — Rating Filter:   Alert me  

BasicAuthentication using HttpClient 4.0

by kash_meu :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi, I am trying to send a GET request to http://abcdef.4y3hsw@hostname.com/abc.php I am not sure how to send request to this url. I am trying like this. HttpGet httpGet = new HttpGet("http://abcdef.4y3hsw@hostname.com/abc.php"); But this does not seem to be working. Any ideas? Regards, Kashif

Re: BasicAuthentication using HttpClient 4.0

by Tobias N. Sasse :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

kash_meu wrote:

> Hi,
>
> I am trying to send a GET request to
> http://abcdef.4y3hsw@.../abc.php I am not sure how to send request
> to this url. I am trying like this.
>
> HttpGet httpGet = new HttpGet("http://abcdef.4y3hsw@.../abc.php");
>
> But this does not seem to be working. Any ideas?
>
> Regards,
> Kashif
>  
You should provide an error message..

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@...
For additional commands, e-mail: httpclient-users-help@...


Re: BasicAuthentication using HttpClient 4.0

by kash_meu :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Tobias N. Sasse wrote:
You should provide an error message..
Thanks. I am not getting any error messages. But the output is not right. When I paste http://abcdef.4y3hsw@hostname.com/abc.php in the browser, I get the right output. But on running the client, the output is not what I am expecting


Re: BasicAuthentication using HttpClient 4.0

by Tobias N. Sasse :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

kash_meu schrieb:
> Thanks. I am not getting any error messages. But the output is not right.
> When I paste http://abcdef.4y3hsw@.../abc.php in the browser, I get
> the right output. But on running the client, the output is not what I am
> expectin

Hi, I have no idea of authentication myself, but you should probably
have a look at this tutorial:

http://hc.apache.org/httpclient-3.x/authentication.html

I know this is for version-3 but perhaps you get an idea how it could
work. I guess the problem is that providing the username and password in
the URL string is not the way httpclient is triggert to provide
authentication to the server?

Regards,
    Tobi


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@...
For additional commands, e-mail: httpclient-users-help@...


Re: BasicAuthentication using HttpClient 4.0

by kash_meu :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Tobi,

Thanks for your help. The link you told is useful for HttpClient 3.1 but there is a lot of difference between 4.0 and 3.1.

I solved my problem. This is the solution.

HttpGet httpGet = new HttpGet(url);
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
                BasicScheme scheme = new BasicScheme();
                Header authorizationHeader = scheme.authenticate(credentials, httpRequest);
               
                httpGet.addHeader(authorizationHeader);


Regards,
Kashif


Tobias N. Sasse wrote:
kash_meu schrieb:
> Thanks. I am not getting any error messages. But the output is not right.
> When I paste http://abcdef.4y3hsw@hostname.com/abc.php in the browser, I get
> the right output. But on running the client, the output is not what I am
> expectin

Hi, I have no idea of authentication myself, but you should probably
have a look at this tutorial:

http://hc.apache.org/httpclient-3.x/authentication.html

I know this is for version-3 but perhaps you get an idea how it could
work. I guess the problem is that providing the username and password in
the URL string is not the way httpclient is triggert to provide
authentication to the server?

Regards,
    Tobi


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

Re: BasicAuthentication using HttpClient 4.0

by olegk :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

kash_meu wrote:

> Hi Tobi,
>
> Thanks for your help. The link you told is useful for HttpClient 3.1 but
> there is a lot of difference between 4.0 and 3.1.
>
> I solved my problem. This is the solution.
>
> HttpGet httpGet = new HttpGet(url);
> UsernamePasswordCredentials credentials = new
> UsernamePasswordCredentials(username, password);
> BasicScheme scheme = new BasicScheme();
> Header authorizationHeader = scheme.authenticate(credentials,
> httpRequest);
>                
>                 httpGet.addHeader(authorizationHeader);
>
>
> Regards,
> Kashif
>
>

While this certainly does the trick for BASIC authentication, more
complex schemes will not work that way.

This is the recommended way of handling authentication with HttpClient 4.0:

http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientAuthentication.java
http://hc.apache.org/httpcomponents-client/tutorial/html/authentication.html

Oleg

>
> Tobias N. Sasse wrote:
>> kash_meu schrieb:
>>> Thanks. I am not getting any error messages. But the output is not right.
>>> When I paste http://abcdef.4y3hsw@.../abc.php in the browser, I
>>> get
>>> the right output. But on running the client, the output is not what I am
>>> expectin
>> Hi, I have no idea of authentication myself, but you should probably
>> have a look at this tutorial:
>>
>> http://hc.apache.org/httpclient-3.x/authentication.html
>>
>> I know this is for version-3 but perhaps you get an idea how it could
>> work. I guess the problem is that providing the username and password in
>> the URL string is not the way httpclient is triggert to provide
>> authentication to the server?
>>
>> Regards,
>>     Tobi
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: httpclient-users-unsubscribe@...
>> For additional commands, e-mail: httpclient-users-help@...
>>
>>
>>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@...
For additional commands, e-mail: httpclient-users-help@...


HandshakeException when using SSL with commons HTTPClient

by Douglas Ferguson-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I get the following exception when trying to request https pages.

I've read online about this a little and there are various suggestions
about how to solve this, however I'm not sure which approach is best.

My use case is to download pages and "scrape" contact information off
the page. Therefore, I'm not really concerned about the validity of
certs, etc, I just want to get the content.

Thanks,

D/

javax.net.ssl.SSLHandshakeException:
sun.security.validator.ValidatorException: PKIX path building failed:
sun.security.provider.certpath.SunCertPathBuilderException: unable to
find valid certification path to requested target
        at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:174)
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal
(SSLSocketImpl.java:1611)
        at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:187)
        at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:181)
        at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate
(ClientHandshaker.java:1035)
        at com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage
(ClientHandshaker.java:124)
        at com.sun.net.ssl.internal.ssl.Handshaker.processLoop
(Handshaker.java:516)
        at com.sun.net.ssl.internal.ssl.Handshaker.process_record
(Handshaker.java:454)
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord
(SSLSocketImpl.java:884)
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake
(SSLSocketImpl.java:1112)
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.writeRecord
(SSLSocketImpl.java:623)
        at com.sun.net.ssl.internal.ssl.AppOutputStream.write
(AppOutputStream.java:59)
        at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:
65)
        at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:123)
        at
org.apache.commons.httpclient.HttpConnection.flushRequestOutputStream
(HttpConnection.java:828)
        at org.apache.commons.httpclient.HttpMethodBase.writeRequest
(HttpMethodBase.java:2116)
        at org.apache.commons.httpclient.HttpMethodBase.execute
(HttpMethodBase.java:1096)
        at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry
(HttpMethodDirector.java:398)
        at org.apache.commons.httpclient.HttpMethodDirector.executeMethod
(HttpMethodDirector.java:171)
        at org.apache.commons.httpclient.HttpClient.executeMethod
(HttpClient.java:397)
        at org.apache.commons.httpclient.HttpClient.executeMethod
(HttpClient.java:323)
        at com.conducive.util.IOUtil.newGetMethod(IOUtil.java:155)
        at org.apache.commons.httpclient.HttpClientTest.testHebrew
(HttpClientTest.java:22)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke
(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at junit.framework.TestCase.runTest(TestCase.java:164)
        at junit.framework.TestCase.runBare(TestCase.java:130)
        at junit.framework.TestResult$1.protect(TestResult.java:106)
        at junit.framework.TestResult.runProtected(TestResult.java:124)
        at junit.framework.TestResult.run(TestResult.java:109)
        at junit.framework.TestCase.run(TestCase.java:120)
        at junit.framework.TestSuite.runTest(TestSuite.java:230)
        at junit.framework.TestSuite.run(TestSuite.java:225)
        at
org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run
(JUnit3TestReference.java:130)
        at org.eclipse.jdt.internal.junit.runner.TestExecution.run
(TestExecution.java:38)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests
(RemoteTestRunner.java:467)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests
(RemoteTestRunner.java:683)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run
(RemoteTestRunner.java:390)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main
(RemoteTestRunner.java:197)
Caused by: sun.security.validator.ValidatorException: PKIX path
building failed:
sun.security.provider.certpath.SunCertPathBuilderException: unable to
find valid certification path to requested target
        at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:285)
        at sun.security.validator.PKIXValidator.engineValidate
(PKIXValidator.java:191)
        at sun.security.validator.Validator.validate(Validator.java:218)
        at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.validate
(X509TrustManagerImpl.java:126)
        at
com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted
(X509TrustManagerImpl.java:209)
        at
com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted
(X509TrustManagerImpl.java:249)
        at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate
(ClientHandshaker.java:1014)
        ... 36 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException:
unable to find valid certification path to requested target
        at sun.security.provider.certpath.SunCertPathBuilder.engineBuild
(SunCertPathBuilder.java:174)
        at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:238)
        at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:280)
        ... 42 more



---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@...
For additional commands, e-mail: httpclient-users-help@...


Re: BasicAuthentication using HttpClient 4.0

by jamesrusso :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

This does not seem to be a correct example for 4.0? Can anyone provide an example of the proper way to do basic authentication in 4.0?

thanks,

-jr

olegk wrote:
While this certainly does the trick for BASIC authentication, more
complex schemes will not work that way.

This is the recommended way of handling authentication with HttpClient 4.0:

http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientAuthentication.java
http://hc.apache.org/httpcomponents-client/tutorial/html/authentication.html

Re: BasicAuthentication using HttpClient 4.0

by jamesrusso :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Sorry for the noise. I was using "HttpClient" as my client (holding an instance of DefaultHttpClient), but I now realize that I need DefaultHttpClient to expose the setCredentialProvider method.

thanks,

-jr


Hello,

This does not seem to be a correct example for 4.0? Can anyone provide an example of the proper way to do basic authentication in 4.0?

thanks,

-jr

olegk wrote:
While this certainly does the trick for BASIC authentication, more
complex schemes will not work that way.

This is the recommended way of handling authentication with HttpClient 4.0:

http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientAuthentication.java
http://hc.apache.org/httpcomponents-client/tutorial/html/authentication.html