java - 如何在 Java 中发送原始 SOAP 请求?

标签 java sockets soap

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

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 发送它,所以我执行以下操作

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("\n * Response");    
String line;
while ((line = in.readLine()) != null) {
     System.out.println(line);
}
in.close();

我收到 HTTP 状态 500 和处理时发生故障的 SOAP 消息。 谢谢。

最佳答案

你能不能这样试试:你需要用你的替换 xmldata:

   try {
        String xmldata = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><soapenv:Envelopey> your reuest</soapenv:Envelope>";
        String hostname = "10.76.243.43";
        int port = 8080;
        InetAddress addr = InetAddress.getByName(hostname);
        Socket sock = new Socket(addr, port);

        //Send header
        String path = "registry/services/xds-iti18";
        BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream(), "UTF-8"));
        wr.write("POST "+path + " HTTP/1.1\r\n");
        wr.write("Host: "+hostname+":"+port+"\r\n");
        wr.write("Content-Length: "+ xmldata.length() + "\r\n");
        wr.write("Content-Type: application/soap+xml; charset=\"utf-8\"\r\n");
        wr.write("\r\n");            //Send data
        wr.write(xmldata);
        wr.flush();

        // Response
        BufferedReader rd = new BufferedReader(new InputStreamReader(sock.getInputStream()));
        String line;
        while ((line = rd.readLine()) != null) {
          System.out.println("Response:"+line);
        }
} catch (Exception e) {
   e.printStackTrace();
}

关于java - 如何在 Java 中发送原始 SOAP 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31286722/

相关文章:

java - 如何在tomcat docker容器中部署多个war?

java - 使用 JsonView Jackson 进行 Json 序列化

HTTP 请求,奇怪的套接字行为

java - 如何通过 Java Socket 向 OSGi 控制台发送命令并接收响应?

javascript - Socket.io 按钮监听器无法在移动设备上工作

soap - Salesforce 证书问题 : sun. security.validator.ValidatorException:PKIX 路径构建失败

c# - SOAP和证书授权

java - 在列表中查找回文

java - Java EE 应用程序中的磁盘存储

php - PHP 中的 SOAP 服务器 - 如何编写 WSDL 文件 - 技巧