使用Java提交XML post请求

时间:2012-10-09 11:47:31

标签: java xml post http-post

我正在尝试将Usurv调查整合到我的网站上。为此,我需要使用HTTP POST向URL http://app.usurv.com/API/Gateway.svc/getcampaignforframe提交XML请求。然后,响应应包含指向调查的唯一URL。

不幸的是我无法让它工作 - 代码编译正确但是当我加载网页时,我得到以下异常:

"WARNING: URL = http://app.usurv.com/API/Gateway.svc/getcampaignforframe
 [Fatal Error] CampaignFrameRequest%3E:6:3: The element type "link" must be terminated by the matching end-tag "</link>"."

我真的很困惑,因为XML甚至没有链接 标签,所以我不确定错误可能来自哪里。有没有人有任何想法可能导致这个以及我如何解决它?

这是Java代码:

public class UsurvSurveyElement extends RenderController 
{
  private static Logger LOG = Logger.getLogger(UsurvSurveyElement.class.getName());
  String xml = "<CampaignFrameRequest xmlns='http://Qsurv/api' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'><PartnerId>236</PartnerId><PartnerWebsiteID>45</PartnerWebsiteID><RespondentID>1</RespondentID><RedirectUrlComplete>http://localhost:8080/eveningstar/home</RedirectUrlComplete><RedirectUrlSkip>http://localhost:8080/eveningstar/home</RedirectUrlSkip></CampaignFrameRequest>";
  String strURL = "http://app.usurv.com/API/Gateway.svc/getcampaignforframe";

  @Override
  public void populateModelBeforeCacheKey(RenderRequest renderRequest, TopModel topModel, ControllerContext controllerContext ) 
  {
    super.populateModelBeforeCacheKey( renderRequest, topModel, controllerContext );  

    PostMethod post = new PostMethod(strURL);

    try 
    { 
        // Specify content type and encoding
        // If content encoding is not explicitly specified
        // ISO-8859-1 is assumed
        post.setRequestHeader(
            "Content-type", "text/xml; charset=ISO-8859-1"); 
        LOG.warning("request headers: " +post.getRequestHeader("Content-type"));

        StringRequestEntity requestEntity = new StringRequestEntity(xml);
        post.setRequestEntity(requestEntity);
        LOG.warning("request entity: " +post.getRequestEntity());

        String response = post.getResponseBodyAsString();
        LOG.warning("XML string = " +  xml); 
        LOG.warning("URL = " +  strURL); 
        topModel.getLocal().setAttribute("thexmlresponse",response);

    }
    catch(Exception e)
    {   
        LOG.warning("Errors while executing postMethod "+ e);
    }  

    try
    {
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        Document document = docBuilder.parse(strURL+xml);
        processNode(document.getDocumentElement());
        LOG.warning("doc output = " + document);
    }
    catch(Exception e)
    {   
        LOG.warning("Errors while parsing XML: "+ e);
    }
} 

private void processNode(Node node) {
    // do something with the current node instead of System.out
    LOG.warning(node.getNodeName());

    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node currentNode = nodeList.item(i);
        if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
            //calls this method for all the children which is Element
            LOG.warning("current node: " + currentNode);
            processNode(currentNode);
        }
    }

}

}

1 个答案:

答案 0 :(得分:0)

这一行看起来很奇怪,你不是要解析响应体吗?

Document document = docBuilder.parse(strURL+xml);

带有字符串参数的parse方法使用此字符串作为URL,因此XML解析器使用GET请求再次连接到服务器。服务器可能正在响应HTML格式的错误消息,导致异常抱怨link元素。

以下内容应该更好:

Document document = docBuilder.parse(new InputSource(new StringReader(response)));