如何在 Java 中发送原始 SOAP 请求? [英] How to send raw SOAP request in Java?

查看:25
本文介绍了如何在 Java 中发送原始 SOAP 请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要向服务器发送原始 SOAP 请求.我的请求看起来像

I need to send a raw SOAP request to server. My request looks like

POST http://10.76.243.43:8080/registry/services/xds-iti18 HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: application/soap+xml;charset=UTF-8
Content-Length: 6383
Host: 10.76.243.43:8080
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:urn="urn:oasis:names:tc:ebxml-regrep:xsd:query:3.0" xmlns:urn1="urn:oasis:names:tc:ebxml-regrep:xsd:rs:3.0" xmlns:urn2="urn:oasis:names:tc:ebxml-regrep:xsd:rim:3.0">
   <soap:Header/>
   <soap:Body>
      <urn:AdhocQueryRequest id="?" comment="?" federated="false" federation="?" startIndex="0" maxResults="-1">
         <!--Optional:-->
         <urn1:RequestSlotList>
            <!--Zero or more repetitions:-->
            <urn2:Slot name="?" slotType="?">
               <urn2:ValueList>
                  <urn2:Value>?</urn2:Value>
               </urn2:ValueList>
            </urn2:Slot>
         </urn1:RequestSlotList>
         <urn:ResponseOption returnType="RegistryObject" returnComposedObjects="false"/>
         <urn2:AdhocQuery id="?" home="?" lid="?" objectType="?" status="?">
            <!--Zero or more repetitions:-->
            <urn2:Slot name="?" slotType="?">
               <urn2:ValueList>
                  <urn2:Value>?</urn2:Value>
               </urn2:ValueList>
            </urn2:Slot>
            <!--Optional:-->
            <urn2:Name>
               <urn2:LocalizedString xml:lang="en-US" charset="UTF-8" value="?"/>
            </urn2:Name>
            <!--Optional:-->
            <urn2:Description>
               <urn2:LocalizedString xml:lang="en-US" charset="UTF-8" value="?"/>
            </urn2:Description>
            <!--Optional:-->
            <urn2:VersionInfo versionName="1.1" comment="?"/>
            <!--Zero or more repetitions:-->
            <urn2:Classification id="?" home="?" lid="?" objectType="?" status="?" classificationScheme="?" classifiedObject="?" classificationNode="?" nodeRepresentation="?">
               <!--Zero or more repetitions:-->
               <urn2:Slot name="?" slotType="?">
                  <urn2:ValueList>
                     <urn2:Value>?</urn2:Value>
                  </urn2:ValueList>
               </urn2:Slot>
               <!--Optional:-->
               <urn2:Name>
                  <urn2:LocalizedString xml:lang="en-US" charset="UTF-8" value="?"/>
               </urn2:Name>
               <!--Optional:-->
               <urn2:Description>
                  <urn2:LocalizedString xml:lang="en-US" charset="UTF-8" value="?"/>
               </urn2:Description>
               <!--Optional:-->
               <urn2:VersionInfo versionName="1.1" comment="?"/>
               <!--Zero or more repetitions:-->
               <urn2:Classification/>
               <!--Zero or more repetitions:-->
               <urn2:ExternalIdentifier id="?" home="?" lid="?" objectType="?" status="?" registryObject="?" identificationScheme="?" value="?">
                  <!--Zero or more repetitions:-->
                  <urn2:Slot name="?" slotType="?">
                     <urn2:ValueList>
                        <urn2:Value>?</urn2:Value>
                     </urn2:ValueList>
                  </urn2:Slot>
                  <!--Optional:-->
                  <urn2:Name>
                     <urn2:LocalizedString xml:lang="en-US" charset="UTF-8" value="?"/>
                  </urn2:Name>
                  <!--Optional:-->
                  <urn2:Description>
                     <urn2:LocalizedString xml:lang="en-US" charset="UTF-8" value="?"/>
                  </urn2:Description>
                  <!--Optional:-->
                  <urn2:VersionInfo versionName="1.1" comment="?"/>
                  <!--Zero or more repetitions:-->
                  <urn2:Classification/>
                  <!--Zero or more repetitions:-->
                  <urn2:ExternalIdentifier/>
               </urn2:ExternalIdentifier>
            </urn2:Classification>
            <!--Zero or more repetitions:-->
            <urn2:ExternalIdentifier id="?" home="?" lid="?" objectType="?" status="?" registryObject="?" identificationScheme="?" value="?">
               <!--Zero or more repetitions:-->
               <urn2:Slot name="?" slotType="?">
                  <urn2:ValueList>
                     <urn2:Value>?</urn2:Value>
                  </urn2:ValueList>
               </urn2:Slot>
               <!--Optional:-->
               <urn2:Name>
                  <urn2:LocalizedString xml:lang="en-US" charset="UTF-8" value="?"/>
               </urn2:Name>
               <!--Optional:-->
               <urn2:Description>
                  <urn2:LocalizedString xml:lang="en-US" charset="UTF-8" value="?"/>
               </urn2:Description>
               <!--Optional:-->
               <urn2:VersionInfo versionName="1.1" comment="?"/>
               <!--Zero or more repetitions:-->
               <urn2:Classification id="?" home="?" lid="?" objectType="?" status="?" classificationScheme="?" classifiedObject="?" classificationNode="?" nodeRepresentation="?">
                  <!--Zero or more repetitions:-->
                  <urn2:Slot name="?" slotType="?">
                     <urn2:ValueList>
                        <urn2:Value>?</urn2:Value>
                     </urn2:ValueList>
                  </urn2:Slot>
                  <!--Optional:-->
                  <urn2:Name>
                     <urn2:LocalizedString xml:lang="en-US" charset="UTF-8" value="?"/>
                  </urn2:Name>
                  <!--Optional:-->
                  <urn2:Description>
                     <urn2:LocalizedString xml:lang="en-US" charset="UTF-8" value="?"/>
                  </urn2:Description>
                  <!--Optional:-->
                  <urn2:VersionInfo versionName="1.1" comment="?"/>
                  <!--Zero or more repetitions:-->
                  <urn2:Classification/>
                  <!--Zero or more repetitions:-->
                  <urn2:ExternalIdentifier/>
               </urn2:Classification>
               <!--Zero or more repetitions:-->
               <urn2:ExternalIdentifier/>
            </urn2:ExternalIdentifier>
            <!--Optional:-->
            <urn2:QueryExpression queryLanguage="?">
               e
               <!--You may enter ANY elements at this point-->
               gero
            </urn2:QueryExpression>
         </urn2:AdhocQuery>
      </urn:AdhocQueryRequest>
   </soap:Body>
</soap:Envelope>

它是由 SOAP UI(使用 Web 服务的 wsdl)生成的标准原始请求,所以如果我将它发送到那里,我会得到 HTTP 代码 200 和一些正常响应.我想使用 Java 发送它,所以我执行以下操作

It is standard raw request generated by SOAP UI (using wsdl of web service), so if I send it there I get HTTP code 200 and some normal response. I want to send it using Java, so I do the following

byte[] encoded = Files.readAllBytes(Paths.get("C:\Users\timofb\Documents\testtest.txt"));//file with my raw request
String soapXml = new String(encoded, StandardCharsets.UTF_8);
Socket socket = new Socket("10.76.243.43", 8080);
BufferedWriter out = new BufferedWriter(
            new OutputStreamWriter(socket.getOutputStream(), "UTF8"));
BufferedReader in = new BufferedReader(
            new InputStreamReader(socket.getInputStream()));
out.write(soapXml);
out.flush();
System.out.println("
 * Response");    
String line;
while ((line = in.readLine()) != null) {
     System.out.println(line);
}
in.close();

我收到 HTTP 状态 500 和处理时发生错误的 SOAP 消息.谢谢.

I get HTTP status 500 and SOAP message that Fault occurred while processing. Thanks.

推荐答案

我找到了答案.这段代码对我有用

I've found an answer. This code worked for me

    byte[] encoded = Files.readAllBytes(Paths.get("C:\Users\timofb\Documents\test.txt"));
    String soapXml = new String(encoded, StandardCharsets.UTF_8);
    SOAPConnectionFactory soapConnectionFactory =
            SOAPConnectionFactory.newInstance();
    java.net.URL endpoint = new URL("http://" + ip + "/cs-repository/services/xds-iti41");
    SOAPConnection connection = soapConnectionFactory.createConnection();
    MessageFactory factory = MessageFactory.newInstance();
    SOAPMessage message = factory.createMessage(new MimeHeaders(), new ByteArrayInputStream(encoded));
    AttachmentPart attachment = message.createAttachmentPart();
    attachment.setContent("sm_content", "text/plain");
    attachment.setContentId("1.9f910338bf0cac0e783bfdec7e53be9237684caa8f8f4e6d@apache.org");
    message.addAttachmentPart(attachment);
    SOAPMessage response = connection.call(message, endpoint);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    response.writeTo(out);
    String strMsg = new String(out.toByteArray());
    return strMsg;

这篇关于如何在 Java 中发送原始 SOAP 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆