Meaningless Drivel is fun!
The moose likes Java in General and the fly likes Sending http POST Data and XML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sending http POST Data and XML" Watch "Sending http POST Data and XML" New topic

Sending http POST Data and XML

Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 751
Hi guys! I'm really stuck right now... I don't have any knowledge on how the backend is implemented (the one I'm sending on) and I am asked to do a Java application that sends http request that includes ordinary parameters like


and xml data which would be like


However, I am receiving an error saying

Content is not allowed in prolog

I tried several ways(the commented ones) to implement this and I've looked at different source codes. I don't want to use any third-party libraries as of this time. Please help. Here is the source code, it's modified a bit just to protect sensitive data.

SCJP 1.5
Joe Ess

Joined: Oct 29, 2001
Posts: 9168

Your URL parameters contain characters that are considered control characters in the HTTP protocol (likely the '/' in your XML). From the URL class doc:

The URL class does not itself encode or decode any URL components according to the escaping mechanism defined in RFC2396. It is the responsibility of the caller to encode any fields, which need to be escaped prior to calling URL, and also to decode any escaped fields, that are returned from URL.

You can use to properly escape your fields.
Be aware that escaping large documents is very inefficient. If you are sending more than a line or two, you should probably look at using a multipart POST to handle your data. See HTTPClient for that functionality on the client side.

[How To Ask Questions On JavaRanch]
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13027
You get the message:
Content is not allowed in prolog

if you feed a character stream to an XML parser and the stream DOES NOT start with a proper XML tag.

In order to treat part of the POSTed data as XML and part as plain text you are going to have to grab the whole thing to a String and separate the XML bits from the plain text.

Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 751
Thanks guys! I could only guess during this time. I can test the URL I'm sending to and so far they only send form values. I don't know if the xml is being wrapped with the proper tags when sent, but when I enter on the textarea I only need to enter some XML tags without the XML DTD.

Also, if you noticed in my code I already use the URLEncoder to escape the XML I send.

it's commented though because I was experimenting on things.

The data being sent to the RPC looks like this

I agree. Here's the link:
subject: Sending http POST Data and XML
It's not a secret anymore!