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 Michael Südkamp <mi...@docware.de> on 2005/01/27 09:24:39 UTC

PUT and basic authentication

Hi,

I have a simple PUT request app working.
Now I want to add Basic Authentication to it. With a GET sample app it
works. But with PUT I get the following error.

27.01.2005 09:15:09
org.apache.commons.httpclient.auth.AuthChallengeProcessor selectAuthScheme
INFO: basic authentication scheme selected
org.apache.commons.httpclient.ProtocolException: Unbuffered entity enclosing
request can not be repeated.
	at
org.apache.commons.httpclient.methods.EntityEnclosingMethod.writeRequestBody
(EntityEnclosingMethod.java:483)
	at
org.apache.commons.httpclient.HttpMethodBase.writeRequest(HttpMethodBase.jav
a:1964)
	at
org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:997
)
	at
org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethod
Director.java:382)
	at
org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDir
ector.java:168)
	at
org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:393)
	at
org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:324)
	at jakarta.commons.httpclient.Put.put(Put.java:49)
	at jakarta.commons.httpclient.Put.main(Put.java:31)
	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:324)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:78)
Exception in thread "main"
Process finished with exit code 1

Here is my code

   HttpClient client = new HttpClient();
    Credentials defaultcreds = new UsernamePasswordCredentials("user",
"pass");
    client.getState().setCredentials(null, null, defaultcreds);

    PutMethod put = new PutMethod("http://linux3/upload/foo.txt");
    RequestEntity entity = new InputStreamRequestEntity(new
FileInputStream("d:\\temp\\foo.txt"));
    put.setRequestEntity(entity);

    client.getHttpConnectionManager().
        getParams().setConnectionTimeout(5000);
    int status = client.executeMethod(put);
    if (status == HttpStatus.SC_OK) {
        System.out.println("Upload complete, response=" +
put.getResponseBodyAsString());
    } else {
      System.err.println("Upload failed, response=" +
HttpStatus.getStatusText(status));
    }

Can anyone help?

Michael


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


Re: Mocking httpclient for unit testing...

Posted by Roland Weber <RO...@de.ibm.com>.
Hi Keith,

have a look at o.a.c.h.protocol.ProtocolSocketFactory.

cheers,
  Roland





"Keith Hyland" <kh...@aol.com> 
27.01.2005 10:57
Please respond to
"HttpClient User Discussion"


To
"HttpClient User Discussion" <ht...@jakarta.apache.org>
cc

Subject
Mocking httpclient for unit testing...






Hi,

I'm trying to wrtie a unit test around a module that uses httpclient.

Ideally I'd like to mock up the actual connection out of my system since 
this is not the functionaliy I want to test.

I've seen other approaches with the basic socket library where 
connections and socket classes were mocked and supplied directly to the 
class under test. That way the mock implementation doesn't make any 
connection at all. But I don't see how to do this with httpclient.

One option would be to mock the complete Httpclient and GetMethod that I 
use.

What approaches do people use for this?

The alternative is that I need to start up a webserver as part of my 
test suite, and get httpClient to connect to that? But this seems a 
little messy.

Cheers,
Keith



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



Mocking httpclient for unit testing...

Posted by Keith Hyland <kh...@aol.com>.
Hi,

I'm trying to wrtie a unit test around a module that uses httpclient.

Ideally I'd like to mock up the actual connection out of my system since 
this is not the functionaliy I want to test.

I've seen other approaches with the basic socket library where 
connections and socket classes were mocked and supplied directly to the 
class under test. That way the mock implementation doesn't make any 
connection at all. But I don't see how to do this with httpclient.

One option would be to mock the complete Httpclient and GetMethod that I 
use.

What approaches do people use for this?

The alternative is that I need to start up a webserver as part of my 
test suite, and get httpClient to connect to that? But this seems a 
little messy.

Cheers,
Keith



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


Re: PUT and basic authentication

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Thu, Jan 27, 2005 at 10:01:11AM +0100, Roland Weber wrote:
> Hello Michael,
> 
> you have to enable preemptive authentication.

Or the so called 'expect-continue' hansdhake

http://jakarta.apache.org/commons/httpclient/3.0/preference-api.html#HTTP%20method%20parameters

Oleg


> Otherwise, HttpClient will send the request without
> basic auth, get an error reply, and cannot retry with
> basic auth because the message body has already
> been sent. See
> 
> HttpClientParams.setAuthenticationPreemptive(...)
> 
> cheers,
>   Roland
> 
> 
> 
> 
> Michael S?dkamp <mi...@docware.de> 
> 27.01.2005 09:24
> Please respond to
> "HttpClient User Discussion"
> 
> 
> To
> <ht...@jakarta.apache.org>
> cc
> 
> Subject
> PUT and basic authentication
> 
> 
> 
> 
> 
> 
> Hi,
> 
> I have a simple PUT request app working.
> Now I want to add Basic Authentication to it. With a GET sample app it
> works. But with PUT I get the following error.
> 
> 27.01.2005 09:15:09
> org.apache.commons.httpclient.auth.AuthChallengeProcessor selectAuthScheme
> INFO: basic authentication scheme selected
> org.apache.commons.httpclient.ProtocolException: Unbuffered entity 
> enclosing
> request can not be repeated.
>                  at
> org.apache.commons.httpclient.methods.EntityEnclosingMethod.writeRequestBody
> (EntityEnclosingMethod.java:483)
>                  at
> org.apache.commons.httpclient.HttpMethodBase.writeRequest(HttpMethodBase.jav
> a:1964)
>                  at
> org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:997
> )
>                  at
> org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethod
> Director.java:382)
>                  at
> org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDir
> ector.java:168)
>                  at
> org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:393)
>                  at
> org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:324)
>                  at jakarta.commons.httpclient.Put.put(Put.java:49)
>                  at jakarta.commons.httpclient.Put.main(Put.java:31)
>                  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:324)
>                  at 
> com.intellij.rt.execution.application.AppMain.main(AppMain.java:78)
> Exception in thread "main"
> Process finished with exit code 1
> 
> Here is my code
> 
>    HttpClient client = new HttpClient();
>     Credentials defaultcreds = new UsernamePasswordCredentials("user",
> "pass");
>     client.getState().setCredentials(null, null, defaultcreds);
> 
>     PutMethod put = new PutMethod("http://linux3/upload/foo.txt");
>     RequestEntity entity = new InputStreamRequestEntity(new
> FileInputStream("d:\\temp\\foo.txt"));
>     put.setRequestEntity(entity);
> 
>     client.getHttpConnectionManager().
>         getParams().setConnectionTimeout(5000);
>     int status = client.executeMethod(put);
>     if (status == HttpStatus.SC_OK) {
>         System.out.println("Upload complete, response=" +
> put.getResponseBodyAsString());
>     } else {
>       System.err.println("Upload failed, response=" +
> HttpStatus.getStatusText(status));
>     }
> 
> Can anyone help?
> 
> Michael
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-user-help@jakarta.apache.org
> 
> 

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


AW: AW: PUT and basic authentication

Posted by Michael Südkamp <mi...@docware.de>.
Yes that works, great!

Thanks a lot

Michael


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


Re: AW: PUT and basic authentication

Posted by Roland Weber <RO...@de.ibm.com>.
Michael Südkamp <mi...@docware.de> wrote on 28.01.2005 
08:49:54:

> One more question: If I supply the wrong credentials, I get again the
> "ProtocolException: unbuffered entity enclosing request can not be 
repeated"
> which is not very helpful. Can I change something so that I get a server
> response "Not authorized" or the like?
> 
Try method.setDoAuthentication(false), that should stop the HttpClient
from automatically handling the 401 response, while it still allows for
preemptive authentication.

hope that helps,
  Roland

AW: PUT and basic authentication

Posted by Michael Südkamp <mi...@docware.de>.
Roland,

Thank you. Yes that works.

One more question: If I supply the wrong credentials, I get again the
"ProtocolException: unbuffered entity enclosing request can not be repeated"
which is not very helpful. Can I change something so that I get a server
response "Not authorized" or the like?

Michael

> -----Ursprüngliche Nachricht-----
> Von: Roland Weber [mailto:ROLWEBER@de.ibm.com]
> Gesendet: Donnerstag, 27. Januar 2005 10:01
> An: HttpClient User Discussion
> Betreff: Re: PUT and basic authentication
>
>
> Hello Michael,
>
> you have to enable preemptive authentication.
> Otherwise, HttpClient will send the request without
> basic auth, get an error reply, and cannot retry with
> basic auth because the message body has already
> been sent. See
>
> HttpClientParams.setAuthenticationPreemptive(...)
>
> cheers,
>   Roland


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


Re: PUT and basic authentication

Posted by Roland Weber <RO...@de.ibm.com>.
Hello Michael,

you have to enable preemptive authentication.
Otherwise, HttpClient will send the request without
basic auth, get an error reply, and cannot retry with
basic auth because the message body has already
been sent. See

HttpClientParams.setAuthenticationPreemptive(...)

cheers,
  Roland




Michael Südkamp <mi...@docware.de> 
27.01.2005 09:24
Please respond to
"HttpClient User Discussion"


To
<ht...@jakarta.apache.org>
cc

Subject
PUT and basic authentication






Hi,

I have a simple PUT request app working.
Now I want to add Basic Authentication to it. With a GET sample app it
works. But with PUT I get the following error.

27.01.2005 09:15:09
org.apache.commons.httpclient.auth.AuthChallengeProcessor selectAuthScheme
INFO: basic authentication scheme selected
org.apache.commons.httpclient.ProtocolException: Unbuffered entity 
enclosing
request can not be repeated.
                 at
org.apache.commons.httpclient.methods.EntityEnclosingMethod.writeRequestBody
(EntityEnclosingMethod.java:483)
                 at
org.apache.commons.httpclient.HttpMethodBase.writeRequest(HttpMethodBase.jav
a:1964)
                 at
org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:997
)
                 at
org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethod
Director.java:382)
                 at
org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDir
ector.java:168)
                 at
org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:393)
                 at
org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:324)
                 at jakarta.commons.httpclient.Put.put(Put.java:49)
                 at jakarta.commons.httpclient.Put.main(Put.java:31)
                 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:324)
                 at 
com.intellij.rt.execution.application.AppMain.main(AppMain.java:78)
Exception in thread "main"
Process finished with exit code 1

Here is my code

   HttpClient client = new HttpClient();
    Credentials defaultcreds = new UsernamePasswordCredentials("user",
"pass");
    client.getState().setCredentials(null, null, defaultcreds);

    PutMethod put = new PutMethod("http://linux3/upload/foo.txt");
    RequestEntity entity = new InputStreamRequestEntity(new
FileInputStream("d:\\temp\\foo.txt"));
    put.setRequestEntity(entity);

    client.getHttpConnectionManager().
        getParams().setConnectionTimeout(5000);
    int status = client.executeMethod(put);
    if (status == HttpStatus.SC_OK) {
        System.out.println("Upload complete, response=" +
put.getResponseBodyAsString());
    } else {
      System.err.println("Upload failed, response=" +
HttpStatus.getStatusText(status));
    }

Can anyone help?

Michael


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