You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by th...@se.abb.com on 2003/04/29 14:04:08 UTC

HTTPClient

Hi everybody!

I'm both new to http communication, http client and I'm trying to send
files with
authentication. However, when sending the request, the request header does
not contain
any authorization header. Shouldn't it? How does it work?
Down below is my code:

          if (this.isAuthorisation())
          {
               if (user == null || password == null)
               {
                    throw new Exception("Authentication is set to true, but
user or password is null");
               }

               client.getState().setCredentials(null, new
UsernamePasswordCredentials(user, password));
               filePost.setDoAuthentication(true);
          }
          FileInputStream stream = new FileInputStream(file);

          filePost.setRequestBody(stream);
          filePost.setRequestHeader("Content-length",
String.valueOf(file.length()));
          status = client.executeMethod(filePost);
          Header[] header = filePost.getRequestHeaders();
          for (int i=0; i < header.length; i++)
          {

               System.out.println(header[i].getName() + " " +
header[i].getValue());
          }
          stream.close();
          statusText = filePost.getStatusText();
          filePost.releaseConnection();
          filePost = null;
          return status;
     }

Here is the reqeust header:
POST http://<host>:5555/collaborator/sap45/fileUpload HTTP/1.1
Content-length: 9411
Host: <host>:5555
User-Agent: Jakarta Commons-HttpClient/2.0M1
X-tid: test2
Content-type: application/x-idoc

Using commons-httpclient-2.0-alpha2

Thanks in advance,

Theis.


Re: HTTPClient

Posted by Oliver Geisser <ol...@gmx.de>.
Hi,

theis.meggerle@se.abb.com wrote:
> Hi everybody!
> 
> I'm both new to http communication, http client and I'm trying to send
> files with
> authentication. However, when sending the request, the request header does
> not contain
> any authorization header. Shouldn't it? How does it work?
[...]

Have a look at the authentication guide:
<http://jakarta.apache.org/commons/httpclient/authentication.html>

The short answer is to use:
   setSystemProperty(Authenticator.PREEMPTIVE_PROPERTY, "true");

If you want to understand why/how this works you must know
that "usually" authentication is done on the "second try".
The first request is sent without authentication headers and gets
a "406 Unauthorized" response which includes a "authentication" header.
Then with the second request the first request is repeated but with
additional authentication information.

For more details read the "HTTP 1.1 Authentication Specification" RFC.

Setting the above property will force HTTPClient to send 
username/password with every request.

Ciao, Olli

--og