如何将soap请求中的字符串数组发送到webservice

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将soap请求中的字符串数组发送到webservice相关的知识,希望对你有一定的参考价值。

我无法将我的请求发送到webservice。

字符串数组不接受并向我显示错误。

我想在soap请求中发送数组字符串,但向我显示错误。

我的网络服务:

    This XML file does not appear to have any style information associated with it. The document tree is shown below.
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:typens="urn:HostIranSmsWebService" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" name="HostIranSmsWebService" targetNamespace="urn:HostIranSmsWebService">
<types>
<xsd:schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:HostIranSmsWebService">
<xsd:complexType name="ArrayOfstring">
<xsd:complexContent>
<xsd:restriction base="soapenc:Array">
<xsd:attribute ref="soapenc:arrayType" wsdl:arrayType="xsd:string[]"/>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="ArrayOfint">
<xsd:complexContent>
<xsd:restriction base="soapenc:Array">
<xsd:attribute ref="soapenc:arrayType" wsdl:arrayType="xsd:int[]"/>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="AccountInfo">
<xsd:all>
<xsd:element name="numbers" type="typens:ArrayOfstring"/>
<xsd:element name="defaultNumber" type="xsd:string"/>
<xsd:element name="receiveUrl" type="xsd:string"/>
<xsd:element name="sent" type="xsd:int"/>
<xsd:element name="received" type="xsd:int"/>
<xsd:element name="credit" type="xsd:int"/>
<xsd:element name="remaining" type="xsd:int"/>
</xsd:all>
</xsd:complexType>
</xsd:schema>
</types>
<message name="send">
<part name="username" type="xsd:string"/>
<part name="password" type="xsd:string"/>
<part name="to" type="xsd:string"/>
<part name="msg" type="xsd:string"/>
<part name="from" type="xsd:string"/>
<part name="time" type="xsd:int"/>
</message>
<message name="sendResponse">
<part name="sendResponse" type="xsd:int"/>
</message>
<message name="sendToMany">
<part name="username" type="xsd:string"/>
<part name="password" type="xsd:string"/>
<part name="to" type="typens:ArrayOfstring"/>
<part name="msg" type="xsd:string"/>
<part name="from" type="xsd:string"/>
<part name="time" type="xsd:int"/>
</message>
<message name="sendToManyResponse">
<part name="sendToManyResponse" type="typens:ArrayOfint"/>
</message>
<message name="deliveryStatus">
<part name="username" type="xsd:string"/>
<part name="password" type="xsd:string"/>
<part name="recipientId" type="xsd:int"/>
</message>
<message name="deliveryStatusResponse">
<part name="deliveryStatusResponse" type="xsd:int"/>
</message>
<message name="verifyReceive">
<part name="username" type="xsd:string"/>
<part name="password" type="xsd:string"/>
<part name="ticket" type="xsd:string"/>
</message>
<message name="verifyReceiveResponse">
<part name="verifyReceiveResponse" type="xsd:boolean"/>
</message>
<message name="accountInfo">
<part name="username" type="xsd:string"/>
<part name="password" type="xsd:string"/>
</message>
<message name="accountInfoResponse">
<part name="accountInfoResponse" type="typens:AccountInfo"/>
</message>
<message name="changePassword">
<part name="username" type="xsd:string"/>
<part name="password" type="xsd:string"/>
<part name="newPassword" type="xsd:string"/>
</message>
<message name="changePasswordResponse">
<part name="changePasswordResponse" type="xsd:void"/>
</message>
<message name="changeTrafficRelay">
<part name="username" type="xsd:string"/>
<part name="password" type="xsd:string"/>
<part name="newURL" type="xsd:string"/>
</message>
<message name="changeTrafficRelayResponse">
<part name="changeTrafficRelayResponse" type="xsd:void"/>
</message>
<portType name="HostIranSmsWebServicePort">
<operation name="send">
<documentation>Send one SMS.</documentation>
<input message="typens:send"/>
<output message="typens:sendResponse"/>
</operation>
<operation name="sendToMany">
<documentation>Send one SMS to many.</documentation>
<input message="typens:sendToMany"/>
<output message="typens:sendToManyResponse"/>
</operation>
<operation name="deliveryStatus">
<documentation>Check SMS delivery status.</documentation>
<input message="typens:deliveryStatus"/>
<output message="typens:deliveryStatusResponse"/>
</operation>
<operation name="verifyReceive">
<documentation>Verify sms validity when you receive an sms</documentation>
<input message="typens:verifyReceive"/>
<output message="typens:verifyReceiveResponse"/>
</operation>
<operation name="accountInfo">
<documentation>Get account info.</documentation>
<input message="typens:accountInfo"/>
<output message="typens:accountInfoResponse"/>
</operation>
<operation name="changePassword">
<documentation>Change account password.</documentation>
<input message="typens:changePassword"/>
<output message="typens:changePasswordResponse"/>
</operation>
<operation name="changeTrafficRelay">
<documentation>Change traffic relay.</documentation>
<input message="typens:changeTrafficRelay"/>
<output message="typens:changeTrafficRelayResponse"/>
</operation>
</portType>
<binding name="HostIranSmsWebServiceBinding" type="typens:HostIranSmsWebServicePort">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="send">
<soap:operation soapAction="urn:HostIranSmsWebServiceAction"/>
<input>
<soap:body use="encoded" namespace="urn:HostIranSmsWebService" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</input>
<output>
<soap:body use="encoded" namespace="urn:HostIranSmsWebService" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</output>
</operation>
<operation name="sendToMany">
<soap:operation soapAction="urn:HostIranSmsWebServiceAction"/>
<input>
<soap:body use="encoded" namespace="urn:HostIranSmsWebService" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</input>
<output>
<soap:body use="encoded" namespace="urn:HostIranSmsWebService" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</output>
</operation>
<operation name="deliveryStatus">
<soap:operation soapAction="urn:HostIranSmsWebServiceAction"/>
<input>
<soap:body use="encoded" namespace="urn:HostIranSmsWebService" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</input>
<output>
<soap:body use="encoded" namespace="urn:HostIranSmsWebService" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</output>
</operation>
<operation name="verifyReceive">
<soap:operation soapAction="urn:HostIranSmsWebServiceAction"/>
<input>
<soap:body use="encoded" namespace="urn:HostIranSmsWebService" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</input>
<output>
<soap:body use="encoded" namespace="urn:HostIranSmsWebService" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</output>
</operation>
<operation name="accountInfo">
<soap:operation soapAction="urn:HostIranSmsWebServiceAction"/>
<input>
<soap:body use="encoded" namespace="urn:HostIranSmsWebService" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</input>
<output>
<soap:body use="encoded" namespace="urn:HostIranSmsWebService" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</output>
</operation>
<operation name="changePassword">
<soap:operation soapAction="urn:HostIranSmsWebServiceAction"/>
<input>
<soap:body use="encoded" namespace="urn:HostIranSmsWebService" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</input>
<output>
<soap:body use="encoded" namespace="urn:HostIranSmsWebService" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</output>
</operation>
<operation name="changeTrafficRelay">
<soap:operation soapAction="urn:HostIranSmsWebServiceAction"/>
<input>
<soap:body use="encoded" namespace="urn:HostIranSmsWebService" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</input>
<output>
<soap:body use="encoded" namespace="urn:HostIranSmsWebService" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</output>
</operation>
</binding>
<service name="HostIranSmsWebService">
<port name="HostIranSmsWebServicePort" binding="typens:HostIranSmsWebServiceBinding">
<soap:address location="http://sms.hostiran.net/webservice/v1/index.php"/>
</port>
</service>
</definitions>

我的代码:

public String sendToMany(String username,String password,Category to,String msg,String from,Long time){
    SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    soapEnvelope.implicitTypes = true;
    soapEnvelope.dotNet = true;
    SoapObject soapReq = new SoapObject("urn:HostIranSmsWebService","sendToMany");
    soapReq.addProperty("username",username);
    soapReq.addProperty("password",password);
    soapEnvelope.addMapping("urn:HostIranSmsWebService","to",new Category().getClass());

    soapReq.addProperty("to",to);

    soapReq.addProperty("msg",msg);
    soapReq.addProperty("from",from);
    soapReq.addProperty("time",time);

    soapEnvelope.setOutputSoapObject(soapReq);
    HttpTransportSE httpTransport = new HttpTransportSE(url);

    try{

            httpTransport.call("urn:HostIranSmsWebService/sendToMany", soapEnvelope);

    }catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}





public class Category extends Vector<String> implements KvmSerializable
{
    /**
     * 
     */
    private static final long serialVersionUID = -1166006770093411055L;

    @Override
    public Object getProperty(int arg0) {
            return this.get(arg0);
    }

    @Override
    public int getPropertyCount() {
            return this.size();
    }

    @Override
    public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) {
            arg2.name = "string";
            arg2.type = PropertyInfo.STRING_CLASS;
    }

    @Override
    public void setProperty(int arg0, Object arg1) {
            this.add(arg1.toString());
    }
    }

我的请求代码:

Category to =new Category();
to.add("1111111111");
to.add("2222222222");
to.add("3333333333");
String res = sendToMany("user1", "pass1", to, "hi", "",Long.valueOf(0));

日食中的错误:

11-14 16:19:51.317: D/msg(681): org.xmlpull.v1.XmlPullParserException: expected: END_TAG {http://schemas.xmlsoap.org/soap/envelope/}Body (position:END_TAG </{http://schemas.xmlsoap.org/soap/envelope/}SOAP-ENV:Fault>@2:199 in java.io.InputStreamReader@44c28540) 

有人可以帮我解决问题吗?

答案

请理解这一点“截至目前,由于某些历史原因,SOAP编码仅存在,因此标准Web服务框架不支持包含Axis2,Metro和CXF的SAOP编码。”这篇文章在http://ssagara.blogspot.com/2011/10/soap-encoding-and-axis2.html中得到了清晰的解释

但即使您想使用它,基本上您的请求应该与您的Web服务相匹配。

<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:HostIranSmsWebService" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
   <soapenv:Header/>
   <soapenv:Body>
      <urn:sendToMany soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
         <username xsi:type="xsd:string">?</username>
         <password xsi:type="xsd:string">?</password>
         <to xsi:type="urn:ArrayOfstring" soapenc:arrayType="xsd:string[]"/>
         <msg xsi:type="xsd:string">?</msg>
         <from xsi:type="xsd:string">?</from>
         <time xsi:type="xsd:int">?</time>
      </urn:sendToMany>
   </soapenv:Body>
</soapenv:Envelope>

阅读此博客,为您的项目生成一个有效的客户端。 http://ssagara.blogspot.com/2011/10/soap-encoding-and-axis2.html。如描述的那样,您可以使用Axis2的wsdl编写脚本来为此wsdl生成一个工作客户端。请仔细阅读博客并使用wsdl2java.sh -uri service.wsdl -d xmlbeans命令获取工作客户端。

您可以使用本教程进一步了解。 http://onjava.com/pub/a/onjava/excerpt/jsoap_5/index1.html?page=2

另一答案

我能够使用以下内容向SOAP(WCF)端点发送XML请求:(发布我的“确切”版本以避免错误翻译)

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <MyServiceAction xmlns="http://myservicehost.com/">
            <somestringparameter>aaaaaaaa-0b11-432e-bf14-d8bb5e52e56a</somestringparameter>
            <anotherstring>dont care</anotherstring>
            <thisisanarray arrayType="string[]"><item type="string">OMS.PartnerService</item></thisisanarray>
        </MyServiceAction >
    </soap:Body>
</soap:Envelope>

对应于(C#)方法:

[OperationContract(Name = "MyServiceAction")]
bool MyServiceAction(string somestringparameter, string anotherstring, string[] thisisanarray) { ... }

就我而言,我不需要在soap信封(<soap:Envelop xmlns:soap...>)和我的Action的命名空间(<MyServiceAction xmlns=...>)之外指定名称空间。我已经看到其他示例为属性值string[]string声明了特定的名称空间,但我不需要它们。

但是从您的Eclipse错误中听起来您的XML生成错误了吗?你必须手动关闭元素吗?

以上是关于如何将soap请求中的字符串数组发送到webservice的主要内容,如果未能解决你的问题,请参考以下文章

如何将xml doc作为字节数组发送到soap服务?

如何在循环中发送多个 SOAP 请求:PHP?

使用node-soap发送正确的soap请求

NSMutableURLRequest 处理soap请求中的特殊字符

Jmeter发送SOAP请求对WebService接口测试

wininet api 将文件发送到 http web 服务