This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Java in General and the fly likes Problem writing to http URL with PUT (and HttpURLConnection)! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem writing to http URL with PUT (and HttpURLConnection)!" Watch "Problem writing to http URL with PUT (and HttpURLConnection)!" New topic
Author

Problem writing to http URL with PUT (and HttpURLConnection)!

Dan Bizman
Ranch Hand

Joined: Feb 25, 2003
Posts: 387
I have the code below. While it creates the "out.txt" file, it won't write any data and returns a 204 "No Content" response. Any idea what's wrong?

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

The response code isn't available until after you've sent the content.



By asking for the response code before you send the content, you're forcing HttpUrlConnection to terminate the request to force the server to send one. Don't ask for the response until after your request is complete.


[Jess in Action][AskingGoodQuestions]
Dan Bizman
Ranch Hand

Joined: Feb 25, 2003
Posts: 387
Yes!

That was it, thanks!

One question, though: why is Http set up that way so that even if you aren't allowed to write to a stream, you still have to wait till after spending all that processing time writing out the bytes of a file before you can find that out?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Well, HTTP was designed as a very simple request/response protocol. So the client sends everything they're going to send, and then the server sends everything they're going to send. Originally, that was always done in a separate network connection -- i.e., one request, one response per connection, by definition. This makes it possible for the client to be completely "dumb" -- a request can come out of a file, and a response go into a file, without the client doing any interpretation whatsoever.
 
wood burning stoves
 
subject: Problem writing to http URL with PUT (and HttpURLConnection)!