从doPost HttpServletRequest读取xml时出错

时间:2013-01-29 17:17:29

标签: java xml servlets

我正在尝试使用http doPost方法读取正在发布的XML文件。使用SAXParser进行解析时会抛出异常:

Content is not allowed in prolog.

doPost代码是:

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);
    }
}

这是我正在测试的JUnit:

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();           
    }

我得到的servlet中的XML输出是这样的:

XML Received %3C%3Fxml+version%3D%221.0%22+encoding%3D%22UTF-8%22%3F%3E%3Cperson%3E%

所以这是在SAXparser中引发异常:

我做错了什么?我是以错误的方式发送XML还是以错误的方式读取它?

1 个答案:

答案 0 :(得分:1)

你假设

httpInData[i]

是一个字符,而它是一个字节。你的内容是UTF-8,这会产生很大的不同。改为使用Reader。

然后,你正在编写你的XML,这是没用的,因为它是一个POST数据。不要编码,只需发送数据。

替换

urlParameters = URLEncoder.encode(buffer.toString(), "UTF-8");

通过

urlParameters = buffer.toString();

此外,名称urlParameter选择不当,因为这是一个帖子正文,不会进入网址,也不是真正的参数。