2012-10-09 75 views
0

我正在嘗試將Usurv調查集成到我的網站。爲此,我需要使用HTTP POST向URL http://app.usurv.com/API/Gateway.svc/getcampaignforframe提交XML請求。然後,響應應包含指向調查的唯一網址。使用Java提交XML發佈請求

可惜我不能得到它的工作 - 代碼編譯正確,但是當我加載網頁,我得到以下異常:

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

} 

}

回答

0

這行看起來真的很奇怪,難道你的意思是解析響應主體呢?

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

parse方法與字符串參數使用該字符串作爲URL,所以XML解析器是連接到服務器再次,使用GET請求。該服務器可能以HTML格式的錯誤消息進行響應,導致出現異常,抱怨link元素。

像下面這樣的東西應該更好地工作:

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

感謝您的。不幸的是,我現在得到一個空指針異常。響應變量爲空,我不知道爲什麼它沒有選擇服務器響應(應該是一個URL)。 – Victoria