• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating an HTTP Post request from Java

 
Karan Rajan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

How do we create an HTTP Post request from Java. I need to send some XML to a server in an HTTP Post request.

Didn't quite know where to post this .. but since the tag line here seems to be " no question too simple or too small " , I thought I'd take a chance.

Many Thanks,
Karan
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't answer your question, but I think I can point you in the right direction: Try searching the Servlets board for "doPost".
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One possibility is to use a Socket that you can write the raw HTTP request to. See the Socket trail in Sun's Java Tutorial for an overview of how to do this. The tutorial will give you the names of the classes that you can use. You should keep the standard API docs for the version of Java you are using bookmarked and handy at all times. They are a great resource, especially when you know which package or class to use but need to look up what methods do particular tasks. The tutorial should help you figure out this latter part then you can refer to the API docs as needed.

Layne
 
Axel Janssen
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another alternative is jakarta.commons.HttpClient:
http://jakarta.apache.org/commons/httpclient/tutorial.html
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this works. I mean, I know it works ... I think it does POST.
 
Marilyn de Queiroz
Sheriff
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This definitely does not belong in a "beginners" forum. I'm moving it to the Servlets forum.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13077
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this works. I mean, I know it works ... I think it does POST.

When you open a URLConnection with a URL that starts with http:// you actually get a HttpURLConnection, which extends URLConnection.
If you will take a look at the documentation for java.net.HttpURLConnection, specifically the setRequestMethod() method, you will see that GET is the default so you are not doing a POST.
As Axel said, HttpClient is handy for this sort of thing.
Bill
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for correcting me. I knew the server didn't care which it was.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hold the presses, that really does send a POST! I just fired up the Wiki and viewed the logs to make sure. The headers are shown verbatim:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic