Is it possible to use the HTTP PUT method with UrlConnection?

C

Chris Berg

I would like to upload a file to a HTTP server using the PUT method. I
know it is unusual, and not many programmers use this method these
days, byt in my particular case it would be better and faster than
POST. It is supported by most servers (including IIS), but my question
is, how do I write the client?

There is one interesting detail here: Normally, one could get finer
access to the protocol by doing:

HttpUrlConnection huc= (HttpUrlConnection) myUrl.getConnection();

as HttpUrlConnection has extra methods, possibly useful for this sort
of thing. But that will fail if you run it on MS's 1.1-VM (IE),
because the class returned is NOT a subclass of HttpUrlConnection.
This prevents me from doing

huc.setRequestMethod("GET");

Has anyone ever tried this? I can't find any useful info on "GET" on
the web.

Chris
 
H

Herman Timmermans

Chris said:
I would like to upload a file to a HTTP server using the PUT method. I
know it is unusual, and not many programmers use this method these
days, byt in my particular case it would be better and faster than
POST. It is supported by most servers (including IIS), but my question
is, how do I write the client?

There is one interesting detail here: Normally, one could get finer
access to the protocol by doing:

HttpUrlConnection huc= (HttpUrlConnection) myUrl.getConnection();

as HttpUrlConnection has extra methods, possibly useful for this sort
of thing. But that will fail if you run it on MS's 1.1-VM (IE),
because the class returned is NOT a subclass of HttpUrlConnection.
This prevents me from doing

huc.setRequestMethod("GET");

Has anyone ever tried this? I can't find any useful info on "GET" on
the web.

Chris

Chris,
Don't use MS's VM, but download the latest from SUN. From an browser user
point of view, the major difference between a GET and POST is that they can
see the GETinformation in the encoded URL that is shown in their browser.
Could you get a little more specific on the problem?
If you are developping the client concerning the PUT method you were
referring to, yes the PUT request method is exactly what you need to
provide a new or replacement document to be stored on the server. A PUT
method asks the server to store the content body to the URI you defined in
your request, and you should get a response back from the server like a
201.
See also :
http://java.sun.com/j2se/1.4.2/docs/api/java/net/HttpURLConnection.html
For more info on the use of PUT, GET, POST Etc..., use google to lookup the
HTTP Protocol.
Hope this helps,
Brgds Herman
 
C

Chris Berg

Don't use MS's VM, but download the latest from SUN. From an browser user
point of view, the major difference between a GET and POST is that they can
see the GETinformation in the encoded URL that is shown in their browser.

I'm sorry, I made a typing error. these lines:

huc.setRequestMethod("GET");

Has anyone ever tried this? I can't find any useful info on "GET" on
[unquote]


were supposed to read:


huc.setRequestMethod("PUT");

Has anyone ever tried this? I can't find any useful info on "PUT" on
[unquote]

I'm sorry, that it made the meaning unclear.

Chris
 
C

Chris Berg

Could you get a little more specific on the problem?
If you are developping the client concerning the PUT method you were



Well, to be more specific: How do I do "PUT" in Java?

Chris
 
H

Herman Timmermans

Chris said:
Don't use MS's VM, but download the latest from SUN. From an browser user
point of view, the major difference between a GET and POST is that they
can see the GETinformation in the encoded URL that is shown in their
browser.

I'm sorry, I made a typing error. these lines:

huc.setRequestMethod("GET");

Has anyone ever tried this? I can't find any useful info on "GET" on
[unquote]


were supposed to read:


huc.setRequestMethod("PUT");

Has anyone ever tried this? I can't find any useful info on "PUT" on
[unquote]

I'm sorry, that it made the meaning unclear.

Chris
Just read the doc as I pointed out - from Java perspective, it is just a
String value you are passing - brgds, Herman
 
R

Robert Olofsson

: Well, to be more specific: How do I do "PUT" in Java?

Create a HttpURLConnection,
call setDoOutput (true),
call setRequestMethod ("PUT")

write data to the stream you get from getOutputStream ()

call getInputStream or any other method that makes the connection
start reading data.

Does that not work?

There is also the route of reading rfc2616, you can find it at
http://www.ietf.org/rfc/rfc2616.txt, and then implement it all
yourself with ordinary sockets. That road is way longer if you
want to be correct and fully HTTP/1.1 compliant (yes, I do know
how much work it is, I have a web proxy that is fully HTTP/1.1
compliant: http://www.khelekore.org/rabbit/ feel free to use
source from it if you want).

/robo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top