• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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: 22821
132
Eclipse IDE Spring 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: 22821
132
Eclipse IDE Spring 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: 4804
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: 22821
132
Eclipse IDE Spring 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

 
If you live in a cold climate and on the grid, incandescent light can use less energy than LED. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic