Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sending http POST Data and XML

 
Timothy Sam
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

myname=john


and xml data which would be like

data=<person><name>john</name></person


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.


 
Joe Ess
Bartender
Pie
Posts: 9258
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Bill
 
Timothy Sam
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


password=password&url=http%3A%2F%2Fgoogle.com%2Fxml%2F&data=%3Cname%3Etest%3C%2Fname%3E%3Citem%3E%3C%2Fitem%3E&action=0&username=test
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic