SOAP + XML - HttpURLConnection中的响应

问题描述 投票:0回答:1

我想从URL获取soap响应,

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:int="http://service.com/integration/">
   <soap:Header>
      <int:Options>
         <int:UpdateLastModified>true</int:UpdateLastModified>
      </int:Options>
   </soap:Header>
   <soap:Body>
      <int:Execute>
         <int:commandRequest xsi:type="int:TaskCreateCommand" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <int:TaskOrder>
               <int:TypeCategory>Request</int:TypeCategory>
               <int:Customer>
                  <int:Id>83</int:Id>
               </int:Customer>
               <int:Items>
                  <int:WoItem>
                     <int:Task>
                        <int:Id>16519</int:Id>
                     </int:Task>
                     <int:Comment>New Task 1</int:Comment>
                  </int:WoItem>
               </int:Items>
            </int:TaskOrder>
         </int:commandRequest>
      </int:Execute>
   </soap:Body>
</soap:Envelope>

代码我尝试过:

String url = "http://url.com/wsdk/thatService.asmx";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type","application/soap+xml");
String xml = ""; //input that above xml 
con.setDoOutput(true);
System.out.println(xml);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(xml);
wr.flush();
wr.close();
String responseStatus = con.getResponseMessage();
System.out.println(responseStatus);

ON:

Internal Server Error
java.io.IOException: Server returned HTTP response code: 500 for URL:

但是,如果我在POSTMAN中运行它,那么它给出了确切的OP:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <ExecuteResponse xmlns="http://service.com/integration/">
            <ExecuteResult xsi:type="TaskResponse">
                <Task>
                    <Id>12367</Id>
                    <ConcurrencyId>1</ConcurrencyId>
                    <Number>KLMNOU</Number>
                    <TypeCategory>Request</TypeCategory>
                </Task>
            </ExecuteResult>
        </ExecuteResponse>
    </soap:Body>
</soap:Envelope>

我在哪里做错了?相同的输入在POSTMAN中正常工作但不能使用代码!

我已经给出了Content-type:application / soap + xml。仍然没有用。我想知道是什么原因引起了这个问题?

java xml-parsing soap-client
1个回答
0
投票

这里有很多潜在的问题,但我怀疑问题是你正在使用DataOutputStream.writeBytes(String),根据文档说明如下:

将字符串作为字节序列写入基础输出流。通过丢弃其高8位,按顺序写出字符串中的每个字符。

我敢打赌服务器不会理解你的数据在其结尾看起来像什么字符集(如果有的话)。请尝试使用OutputStreamWriter.write(String)

OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
wr.write(xml);
© www.soinside.com 2019 - 2024. All rights reserved.