简体   繁体   English

JAVA中的SOAP请求。 如何通过HEADER

[英]SOAP request in JAVA. How to pass HEADER

I have a problem with create SOAP request. 我在创建SOAP请求时遇到问题。 I have a works request from command line with usage curl 我在命令行中有使用curl的工作请求

<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
 <s:Body>
  <u:SetVolume xmlns:u="urn:schemas-upnp-org:service:RenderingControl:1">
   <InstanceID>0</InstanceID>
   <Channel>Master</Channel>
   <DesiredVolume>20</DesiredVolume>
  </u:SetVolume>
 </s:Body>
</s:Envelope> | curl -v -d @-  -H 'SOAPAction: "urn:schemas-upnp-org:service:RenderingControl:1#SetVolume"'  -H 'content-type: text/xml; charset="utf-8"'  http://192.168.0.172:1400/MediaRenderer/RenderingControl/Control

I tried crate equivalent in JAVA 我在JAVA中尝试了等效的箱子

  import javax.xml.namespace.QName;
import javax.xml.soap.*;

public class SOAPClientSAAJ {


    public static void main(String args[]) {
        try {
                MessageFactory mf = MessageFactory.newInstance();
                SOAPMessage sm = mf.createMessage();
                SOAPHeader sh = sm.getSOAPHeader();
                SOAPBody sb = sm.getSOAPBody();
                sh.detachNode();

                QName bodyName = new QName("urn:schemas-upnp-org:service:RenderingControl:1#SetVolume", "SetVolume", "u");
                SOAPBodyElement bodyElement = sb.addBodyElement(bodyName);

                QName instanceID = new QName("InstanceID");
                QName channel = new QName("Channel");
                QName volumeLevel = new QName("DesiredVolume");

                SOAPElement qInstanceID = bodyElement.addChildElement(instanceID);
                SOAPElement qChannel = bodyElement.addChildElement(channel);
                SOAPElement qVolumeLevel = bodyElement.addChildElement(volumeLevel);

                qInstanceID.addTextNode("0");
                qChannel.addTextNode("Master");
                qVolumeLevel.addTextNode("30");

                sm.writeTo(System.out);
                System.out.println();
        } catch(Exception e) {

        }
    }
}

This give me build request 这给我建立请求

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Body>
      <u:SetVolume xmlns:u="urn:schemas-upnp-org:service:RenderingControl:1#SetVolume">
         <InstanceID>0</InstanceID>
         <Channel>Master</Channel>
         <DesiredVolume>30</DesiredVolume>
      </u:SetVolume>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Unfortunately this don't want to works. 不幸的是,这不想起作用。 I supose that problem is in HEADER, but actualy I don't know how to pass them in other way to my request. 我认为问题出在HEADER中,但实际上我不知道如何将它们以其他方式传递给我的请求。 I also try solution from, but also get error 我也尝试从解决方案,但也得到错误

SOAP request to WebService with java 使用Java对WebService的SOAP请求

It seems like you are setting wrong QName. 好像您设置了错误的QName。 What you are setting is action so apart from that your generated soap envelope is fine I would suggest you to try calling this using SOAP UI. 您所设置的是动作,因此除了生成的肥皂包很好之外,我建议您尝试使用SOAP UI进行调用。

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

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