• 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

How to submit POST request without an input variable

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
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
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
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch!
 
abhishekj jain
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic