请求被 CDATA 包装

发布于 2025-01-08 08:09:24 字数 2661 浏览 0 评论 0原文

我正在开发 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 内部服务器错误,所以我不确定我是否更接近。任何建议仍然非常感谢

I am working on a java client for WCF, and have the template worked out pretty good. I was initially using the web service client project from eclipse but then found out the libraries needed aren't supported on the android platform. I was then going to use ksoap, but it gave me a lot of issues, so I got a copy of a working soap request

<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>

And decided to make a template that would take in the value and stick it where the 42 goes. When I print it out, it looks like it should work great, but I noticed when I trace that my request is wrapped in CDATA tags.

<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>

I am not sure why it is wrapped in CDATA, I am using a HttpURLConnection to make and send the connection. The code that handles that is shown below.

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

The request variable is what is shown above, at least what is wrapped in the CDATA tags. When I call System.out.println(connection.getResponseMessage()); then it tells me Unsupported Media Type.
I am using Text as the messageEncoding in my binding configuration for the WCF server

Does anyone have any suggestions as to how I could get my java client to send the data correctly rather than in a cdata wrapper?

Thanks
Nick Long

edit:
I changed this line

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

to this

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

like it probably should have been to start with. I now get a 500 internal server error, so I'm not sure if I am any closer or not. Any suggestions are still really appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蔚蓝源自深海 2025-01-15 08:09:24

我所做的第一个更改是:

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 的图像共享客户端。

The first change I made was this:

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

to this

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

That was what got rid of the 415 error. After that I had the 500 error. What I did to fix that was I took this line

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

and removed it, making my template this:

<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>

The reason, I believe (someone please correct me if I am wrong) that I was getting the 500 error was that I had this line and the line I removed:

connection.setRequestProperty("SOAPAction",SOAP_ACTION);

After making those changes, my client works great, and I have been able to take it to an image sharing client for the android.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文