【问题标题】:Requests are getting wrapped with CDATA请求被 CDATA 包裹
【发布时间】:2012-02-21 20:15:28
【问题描述】:

我正在为 WCF 开发一个 java 客户端,并且模板的效果非常好。我最初使用的是 Eclipse 中的 Web 服务客户端项目,但后来发现所需的库在 android 平台上不受支持。然后我打算使用 ksoap,但它给了我很多问题,所以我得到了一份工作肥皂请求的副本

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/ITransferService/getInt</Action>
  </s:Header>
  <s:Body>
    <getInt xmlns="http://tempuri.org/">
      <i>42</i>
    </getInt>
  </s:Body>
</s:Envelope>

并决定制作一个模板来接收该值并将其粘贴到 42 所在的位置。当我打印出来时,它看起来应该很好用,但是当我跟踪我的请求时发现我的请求包含在 CDATA 标记中。

<MessageLogTraceRecord><![CDATA[<?xml version="1.0" encoding="utf-8"?>
    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
        <s:Header>
            <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/ITransferService/getInt</Action>
        </s:Header>
        <s:Body>
            <getInt xmlns="http://tempuri.org/">
                <i>100</i>
            </getInt>
        </s:Body>
    </s:Envelope>
]]></MessageLogTraceRecord>

我不确定为什么它被包裹在 CDATA 中,我正在使用 HttpURLConnection 来建立和发送连接。处理的代码如下所示。

private static void sendRequest(String request) throws Exception
    {
        URL u = new URL(DEFAULT_SERVER);
        URLConnection uc = u.openConnection();
        HttpURLConnection connection = (HttpURLConnection)uc;

        connection.setRequestProperty("content-type", "text/html; charset=utf8");

        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("SOAPAction",SOAP_ACTION);

        OutputStream out = connection.getOutputStream();
        Writer wout = new OutputStreamWriter(out);

        wout.write(request);
        wout.flush();
        wout.close();
        System.out.println(connection.getResponseMessage());
    }

请求变量如上所示,至少包含在 CDATA 标记中。当我调用 System.out.println(connection.getResponseMessage());然后它告诉我不支持的媒体类型。 我在 WCF 服务器的绑定配置中使用 Text 作为 messageEncoding

对于如何让我的 java 客户端正确发送数据而不是在 cdata 包装器中发送数据,是否有人有任何建议?

谢谢 尼克·朗

编辑: 我改变了这一行

connection.setRequestProperty("content-type", "text/html; charset=utf8");

到这里

connection.setRequestProperty("content-type", "text/xml");

可能应该是这样开始的。我现在收到 500 内部服务器错误,所以我不确定我是否更接近。任何建议仍然非常感谢

【问题讨论】:

  • 抱歉,按我的意思点击提交,不知道是怎么回事
  • 对于那些否决不完整问题以纠正该问题的人来说,这当然是礼貌的。

标签: java wcf soap


【解决方案1】:

我做的第一个改变是这样的:

connection.setRequestProperty("content-type", "text/html; charset=utf8");

到这里

connection.setRequestProperty("content-type", "text/xml");

这就是摆脱 415 错误的原因。之后我遇到了 500 错误。我所做的就是解决这个问题

<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/ITransferService/getInt</Action>

并删除它,使我的模板成为:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Header>
    </s:Header>
    <s:Body>
        <getInt xmlns="http://tempuri.org/">
            <i>%val%</i>
        </getInt>
    </s:Body>
</s:Envelope>

我相信(如果我错了,请纠正我)我收到 500 错误的原因是我有这条线并且我删除了这条线:

connection.setRequestProperty("SOAPAction",SOAP_ACTION);

进行这些更改后,我的客户端运行良好,我已经能够将其带到 android 的图像共享客户端。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-18
    • 1970-01-01
    • 1970-01-01
    • 2014-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-02
    相关资源
    最近更新 更多