Reputation: 205
I am trying to read the XML file being posted using http doPost
method. While parsing using SAXParser it throws an Exception:
Content is not allowed in prolog.
The doPost code is:
protected void doPost(HttpServletRequest request, HttpServletResponse response)
{
ServletInputStream httpIn = request.getInputStream();
byte[] httpInData = new byte[request.getContentLength()];
StringBuffer readBuffer = new StringBuffer();
int retVal = -1;
while ((retVal = httpIn.read(httpInData)) != -1)
{
for (int i=0; i<retVal; i++)
{
readBuffer.append(Character.toString((char)httpInData[i]));
}
}
System.out.println("XML Received" + readBuffer);
try
{
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
ByteArrayInputStream inputStream = new ByteArrayInputStream(
readBuffer.toString().getBytes("UTF-8"));
final XmlParser xmlParser = new XmlParser();
parser.parse(inputStream, xmlParser);
}
catch (Exception e)
{
System.out.println("Exception parsing the xml request" + e);
}
}
This is the JUnit I am testing with:
public static void main(String args[])
{
StringBuffer buffer = new StringBuffer();
buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
buffer.append("<person>");
buffer.append("<name>abc</name>");
buffer.append("<age>25</age>");
buffer.append("</person>");
try
{
urlParameters = URLEncoder.encode(buffer.toString(), "UTF-8");
}
catch (Exception e1)
{
e1.printStackTrace();
}
String targetURL = "http://localhost:8888/TestService";
URL url;
HttpURLConnection connection = null;
try
{
//Create connection
url = new URL(targetURL);
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/xml");
connection.setRequestProperty("Content-Length", "" +
Integer.toString(urlParameters.getBytes("UTF-8").length));
connection.setRequestProperty("Content-Language", "en-US");
connection.setUseCaches (false);
connection.setDoInput(true);
connection.setDoOutput(true);
//Send request
DataOutputStream wr = new DataOutputStream (
connection.getOutputStream ());
wr.writeBytes (urlParameters);
wr.flush ();
wr.close ();
}
catch (Exception e)
{
e.printStackTrace();
}
The XML output in the servlet that I am getting is something like this:
XML Received %3C%3Fxml+version%3D%221.0%22+encoding%3D%22UTF-8%22%3F%3E%3Cperson%3E%
So this is throwing an exception in SAXparser:
What am I doing wrong? Am I sending the XML in wrong way or reading it wrong way?
Upvotes: 1
Views: 2460
Reputation: 39406
You assume
httpInData[i]
is a char, while it is a byte. Your content being UTF-8, that makes a big difference. Use a Reader instead.
Then, you are URLEncoding your XML, which is useless, as it is a POST data. Don't encode it, simply send the data.
replace
urlParameters = URLEncoder.encode(buffer.toString(), "UTF-8");
by
urlParameters = buffer.toString();
Also, the name urlParameter is poorly chosen, as this is a single post body, doesn't go in the url, and isn't really a parameter.
Upvotes: 1