• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to submit POST request without an input variable  RSS feed

 
abhishekj jain
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
I have to post an XML to a URL. i dont have a POST variable to carry the value,
I have seen some code on net but they all need variable,
pl. advice,

thanks
abhi
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The usual practice is to send any document such as your XML as the body of the request, assuming you are creating the request from a Java application.

Or - are you talking about POSTing from an HTML form?

Bill
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch!
 
abhishekj jain
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
i dont want to use a form rather i want to use java api for this purpose. Can someone here paste some code to help.

Thanks
abhi
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Call setDoOutput(true), then simply write the XML document to the connection's output stream.

And with connection, I of course mean a URLConnection instance that you obtain from a URL instance.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an example - note the following points:
1. you get a HttpURLConnection because the serviceURL starts with "http:"
2. setDoOutput(true) is required, setDoInput(true ) if you are going to read the response
3. setting the Content-Type request header may be required depending on the receiving service
4. setting the Content-Length is always a good idea
5. flush() the output stream, do NOT close it because that will close the connection



Carefully read the java.io.HttpURLConnection and URLConnection Javadocs.

Bill

 
Pat Farrell
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abhishekj jain wrote:i dont want to use a form rather i want to use java api for this purpose. Can someone here paste some code to help.

It is possible, as folks have posted upthread. But I strongly recommend that you not do this. Instead, use the Apache HTTP client library. There are a huge number of subtle things that doing it by hand will skip.

Object oriented programming is about code-reuse. Use the Apache code.
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William, you forgot one thing - to call either getResponseCode() or getInputStream(). The return value can be ignored, but without it the actual request won't be made.

Pat Farrell wrote:
abhishekj jain wrote:i dont want to use a form rather i want to use java api for this purpose. Can someone here paste some code to help.

It is possible, as folks have posted upthread. But I strongly recommend that you not do this. Instead, use the Apache HTTP client library. There are a huge number of subtle things that doing it by hand will skip.

Object oriented programming is about code-reuse. Use the Apache code.

I agree, but perhaps this is a school project which disallows the use of anything outside the core API. Just recently there was a thread from someone who wasn't even allowed to use (Http)URLConnection, but had to do it all with Sockets manually.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I didn't forget - that was not intended to be a complete request/response handling method. Since I clipped it out of working code and didn't what to explain my response handler.

However, that is an interesting question - I think the request will be made even if you don't getResponseCode or getInputStream because it does flush the output stream. I'm guessing the connection will eventually time out but that is just a guess and I would be interested in any example.

Bill

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!