简体   繁体   English

SOAP + XML - HttpURLConnection中的响应

[英]SOAP +XML - Response in HttpURLConnection

I am trying to get soap response from URL, 我想从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>

Code I have tried : 代码我尝试过:

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

OP : OP:

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

But If I run this in POSTMAN then it gives exact OP: 但是,如果我在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>

where Am I doing a mistake? 我在哪里做错了? Same input working fine in POSTMAN but not working using code! 相同的输入在POSTMAN中正常工作但不能使用代码!

I have given Content-type : application/soap+xml in both side. 我已经给出了Content-type:application / soap + xml。 Still its not working. 仍然没有用。 I am wondering what makes this issue? 我想知道是什么原因引起了这个问题?

There are a lot of potential problems here, but I suspect the issue is that you're using DataOutputStream.writeBytes(String) , which according to the documentation says the following: 这里有很多潜在的问题,但我怀疑问题是你正在使用DataOutputStream.writeBytes(String) ,根据文档说明如下:

Writes out the string to the underlying output stream as a sequence of bytes. 将字符串作为字节序列写入基础输出流。 Each character in the string is written out, in sequence, by discarding its high eight bits. 通过丢弃其高8位,按顺序写出字符串中的每个字符。

I'll bet the server doesn't understand whatever character set (if any) your data looks like on its end. 我敢打赌服务器不会理解你的数据在其结尾看起来像什么字符集(如果有的话)。 Try OutputStreamWriter.write(String) instead: 请尝试使用OutputStreamWriter.write(String)

OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
wr.write(xml);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM