aspose file tools*
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
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sending http POST Data and XML" Watch "Sending http POST Data and XML" New topic
Author

Sending http POST Data and XML

Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
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.




SCJP 1.5
http://devpinoy.org/blogs/lamia/ - http://everypesocounts.com/
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8910
    
    8

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.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12789
    
    5
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

Joined: Sep 18, 2005
Posts: 746
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sending http POST Data and XML