This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques and have Mario Casciaro & Luciano Mammino on-line!
See this thread for details.
Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Sending http POST Data and XML

 
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
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
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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic