• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Creating an HTTP Post request from Java

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another alternative is jakarta.commons.HttpClient:
http://jakarta.apache.org/commons/httpclient/tutorial.html
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this works. I mean, I know it works ... I think it does POST.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This definitely does not belong in a "beginners" forum. I'm moving it to the Servlets forum.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for correcting me. I knew the server didn't care which it was.
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic