This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
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.
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 java.net.URLEncoder 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.
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.
Joined: Sep 18, 2005
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.