Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java提交XML post请求_Java_Xml_Post_Http Post - Fatal编程技术网

使用Java提交XML post请求

使用Java提交XML post请求,java,xml,post,http-post,Java,Xml,Post,Http Post,我正在尝试将Usurv调查整合到我的网站中。为此,我需要使用HTTPPOST向URL提交一个XML请求。然后,响应应该包含指向调查的唯一URL 不幸的是,我无法让它工作-代码编译正确,但当我加载网页时,我得到以下异常: "WARNING: URL = http://app.usurv.com/API/Gateway.svc/getcampaignforframe [Fatal Error] CampaignFrameRequest%3E:6:3: The element type "link"

我正在尝试将Usurv调查整合到我的网站中。为此,我需要使用HTTPPOST向URL提交一个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>"."
“警告:URL=http://app.usurv.com/API/Gateway.svc/getcampaignforframe
[致命错误]活动框架请求%3E:6:3:元素类型“链接”必须由匹配的结束标记“”终止
我真的很困惑,因为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);
        }
    }

}
公共类UservSurveyElement扩展了RenderController
{
私有静态记录器LOG=Logger.getLogger(UsurvSurveyElement.class.getName());
String xml=“236451http://localhost:8080/eveningstar/homehttp://localhost:8080/eveningstar/home";
字符串strURL=”http://app.usurv.com/API/Gateway.svc/getcampaignforframe";
@凌驾
public void populateModelBeforeCacheKey(RenderRequest RenderRequest、TopModel TopModel、ControllerContext ControllerContext)
{
super.populateModelBeforeCacheKey(renderRequest、topModel、controllerContext);
PostMethod post=新的PostMethod(strURL);
尝试
{ 
//指定内容类型和编码
//如果未明确指定内容编码
//假定为ISO-8859-1
post.setRequestHeader(
“内容类型”、“文本/xml;字符集=ISO-8859-1”);
LOG.warning(“请求头:”+post.getRequestHeader(“内容类型”);
StringRequestEntity requestEntity=新的StringRequestEntity(xml);
post.setRequestEntity(requestEntity);
警告(“请求实体:+post.getRequestEntity());
字符串响应=post.getResponseBodyAsString();
LOG.warning(“XML字符串=“+XML”);
日志警告(“URL=“+strURL”);
topModel.getLocal().setAttribute(“thexmlresponse”,response);
}
捕获(例外e)
{   
日志警告(“执行postMethod时出错”+e);
}  
尝试
{
DocumentBuilderFactory docBuilderFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder=docBuilderFactory.newDocumentBuilder();
Document=docBuilder.parse(strURL+xml);
processNode(document.getDocumentElement());
日志警告(“文档输出=”+文档);
}
捕获(例外e)
{   
LOG.warning(“解析XML时出错:+e”);
}
} 
私有void进程节点(节点节点){
//使用当前节点而不是System.out执行某些操作
LOG.warning(node.getNodeName());
NodeList NodeList=node.getChildNodes();
for(int i=0;i

}

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

Document document = docBuilder.parse(strURL+xml);
带有字符串参数的parse方法使用该字符串作为URL,因此XML解析器使用GET请求再次连接到服务器。服务器可能正在响应HTML格式的错误消息,导致异常抱怨
链接
元素

类似于以下内容的内容应该更有效:

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

谢谢你。不幸的是,我现在得到一个空指针异常。response变量为null,我不确定它为什么不接收服务器响应(应该是URL)。