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 Philip MacIver <pm...@arishi.com> on 2006/06/19 14:43:41 UTC

HTTP PUT

Hi I was wondering if someone could help with this problem.
Basically I have written this lava program to communicate with an apache 
  server. Everything works fine with the program, it it does not work 
exactly in the way I would like it to. What I would like it to send a 
request to PUT a file up onto the server and then get response back from 
the server saying that the username password entered is correct, and the 
(and only then) should the file upload start. My problem is that at the 
moment whether the username/password is right or wrong the file upload 
happens and if the credentials are wrong it get discarded. Now this 
isn't a problem with small files but this application will be dealing 
with files 100s of MB large so it will cause unneeded delay and file 
uploading if I can't change.

Here is the main part of the program

--8<--
public void doPut(String server, int port, String fileName, String 
username, String password) {
		HttpClient client;
		Credentials defaultcreds;
		ArrayList authPrefs;
		PutMethod put = null;;
		Header[] he;
		
		try {
			client = new HttpClient();
		
			authPrefs = new ArrayList(2);
			authPrefs.add(AuthPolicy.BASIC);
			authPrefs.add(AuthPolicy.DIGEST);

			client.getState().setCredentials(new 			AuthScope(server, port, 
AuthScope.ANY_REALM), new UsernamePasswordCredentials(username, password));
			client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, 
authPrefs);
			client.getParams().setAuthenticationPreemptive(true);

			put = !server.endsWith("/") ? new PutMethod(server + "/" + fileName) 
: new PutMethod(server + fileName);
			put.setDoAuthentication(true);
		 
put.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE,true);
			put.setRequestEntity(new FileRequestEntity(new File(fileName)));
			put.addRequestHeader(new Header("Authorization",new 
BasicScheme().authenticate(new UsernamePasswordCredentials(username, 
password), put)));
			
			System.out.println("FIRST: " + client.executeMethod(put) + " " + 
put.getStatusLine());
		 
System.out.println("=====================================================");
			System.out.println("Request");
			he = put.getRequestHeaders();
			for (int i = 0; i < he.length; i++) {
				System.out.print(he[i]);
			}
		 
System.out.println("=====================================================");
		 
System.out.println("=====================================================");
			System.out.println("Response");
			he = put.getResponseHeaders();
			for (int i = 0; i < he.length; i++) {
				System.out.print(he[i]);
			}
		 
System.out.println("=====================================================");
						
			System.out.println("\n\nSECOND: " + client.executeMethod(put) + " " + 
put.getStatusLine());
		 
System.out.println("=====================================================");
			System.out.println("Request");
			he = put.getRequestHeaders();
			for (int i = 0; i < he.length; i++) {
				System.out.print(he[i]);
			}
		 
System.out.println("=====================================================");
		 
System.out.println("=====================================================");
			System.out.println("Response");
			he = put.getResponseHeaders();
			for (int i = 0; i < he.length; i++) {
				System.out.print(he[i]);
			}
		 
System.out.println("=====================================================");
		} catch(HttpException e) {
			System.out.println("Error " + e);
			e.printStackTrace();
		} catch(IOException e) {
			System.out.println("Error " + e);
			e.printStackTrace();
		} finally {
			if (put != null) {
				put.releaseConnection();
			}
		}
	}
--8<--

Thanks for any help given in advanced.


Philip Maciver

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


Re: HTTP PUT

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Mon, 2006-06-19 at 16:51 +0100, Philip MacIver wrote:
> What I really want to know is how to add the content body to the request 
> after I have got the 100-continue, because at the moment when I try and 
> do that it doesn't add the file. And I am not sure how you do that, and 
> I cannot find an examples on the web  at all.

Well, that just cannot be done. One must fully provide content body
parameters prior to committing the request in order for HttpClient to be
able to set up the request head right and generate correct
Content-Length, Transfer-Encoding, Content-Type and such headers

Oleg


> 
> Roland Weber wrote:
> > Hello Philip,
> > 
> >> (and only then) should the file upload start. My problem is that at the
> >> moment whether the username/password is right or wrong the file upload
> >> happens and if the credentials are wrong it get discarded. Now this
> >> isn't a problem with small files but this application will be dealing
> >> with files 100s of MB large so it will cause unneeded delay and file
> >> uploading if I can't change.
> > 
> > you've already enabled the expect-continue handshake. If the server
> > still doesn't verify the credentials before sending the "continue",
> > you'll have to split your actions into two requests: the first to
> > make sure that the credentials are ok, the second for the upload.
> > You could/should also complain to whomever it is that implemented
> > the server that it doesn't work as it should. Note however that the
> > HTTP specification does not *require* servers to check credentials
> > before sending the "continue". It only suggests that they should.
> > 
> > hope that helps,
> >   Roland
> > 
> > ---------------------------------------------------------------------
> > 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
> 
> 


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


Re: HTTP PUT

Posted by Philip MacIver <pm...@arishi.com>.
What I really want to know is how to add the content body to the request 
after I have got the 100-continue, because at the moment when I try and 
do that it doesn't add the file. And I am not sure how you do that, and 
I cannot find an examples on the web  at all.

Roland Weber wrote:
> Hello Philip,
> 
>> (and only then) should the file upload start. My problem is that at the
>> moment whether the username/password is right or wrong the file upload
>> happens and if the credentials are wrong it get discarded. Now this
>> isn't a problem with small files but this application will be dealing
>> with files 100s of MB large so it will cause unneeded delay and file
>> uploading if I can't change.
> 
> you've already enabled the expect-continue handshake. If the server
> still doesn't verify the credentials before sending the "continue",
> you'll have to split your actions into two requests: the first to
> make sure that the credentials are ok, the second for the upload.
> You could/should also complain to whomever it is that implemented
> the server that it doesn't work as it should. Note however that the
> HTTP specification does not *require* servers to check credentials
> before sending the "continue". It only suggests that they should.
> 
> hope that helps,
>   Roland
> 
> ---------------------------------------------------------------------
> 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


Re: HTTP PUT

Posted by Philip MacIver <pm...@arishi.com>.
Ok this could well be the case, but could there also be something wrong 
with my code? Because I have tried all the permutations and it still not 
behaving correctly.


Roland Weber wrote:
> Hello Philip,
> 
>> (and only then) should the file upload start. My problem is that at the
>> moment whether the username/password is right or wrong the file upload
>> happens and if the credentials are wrong it get discarded. Now this
>> isn't a problem with small files but this application will be dealing
>> with files 100s of MB large so it will cause unneeded delay and file
>> uploading if I can't change.
> 
> you've already enabled the expect-continue handshake. If the server
> still doesn't verify the credentials before sending the "continue",
> you'll have to split your actions into two requests: the first to
> make sure that the credentials are ok, the second for the upload.
> You could/should also complain to whomever it is that implemented
> the server that it doesn't work as it should. Note however that the
> HTTP specification does not *require* servers to check credentials
> before sending the "continue". It only suggests that they should.
> 
> hope that helps,
>   Roland
> 
> ---------------------------------------------------------------------
> 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


Re: HTTP PUT

Posted by Roland Weber <ht...@dubioso.net>.
Hello Philip,

> (and only then) should the file upload start. My problem is that at the
> moment whether the username/password is right or wrong the file upload
> happens and if the credentials are wrong it get discarded. Now this
> isn't a problem with small files but this application will be dealing
> with files 100s of MB large so it will cause unneeded delay and file
> uploading if I can't change.

you've already enabled the expect-continue handshake. If the server
still doesn't verify the credentials before sending the "continue",
you'll have to split your actions into two requests: the first to
make sure that the credentials are ok, the second for the upload.
You could/should also complain to whomever it is that implemented
the server that it doesn't work as it should. Note however that the
HTTP specification does not *require* servers to check credentials
before sending the "continue". It only suggests that they should.

hope that helps,
  Roland

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