Android KSoap2不是有效的SOAP

Android KSoap2不是有效的SOAP,android,soap,Android,Soap,我正试图将Android与KSOAP2结合起来发布到我自己的测试soap服务器(C#) 现在我从SOAP服务器获得了规范,它期望: POST /SharingpointCheckBarcode.asmx HTTP/1.1 Host: awc.test.trin-it.nl Content-Type: text/xml; charset=utf-8 Content-Length: length SOAPAction: "http://tempuri.org/checkBarcode" <?

我正试图将Android与KSOAP2结合起来发布到我自己的测试soap服务器(C#)

现在我从SOAP服务器获得了规范,它期望:

POST /SharingpointCheckBarcode.asmx HTTP/1.1
Host: awc.test.trin-it.nl
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/checkBarcode"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:xsd="http://www.w3.org/2001/XMLSchema"     xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <AuthHeader xmlns="http://tempuri.org/">
      <username>string</username>
      <password>string</password>
    </AuthHeader>
  </soap:Header>
  <soap:Body>
    <checkBarcode xmlns="http://tempuri.org/">
      <barcode>string</barcode>
    </checkBarcode>
  </soap:Body>
</soap:Envelope>  
POST/SharingpointCheckBarcode.asmx HTTP/1.1
主持人:awc.test.trin-it.nl
内容类型:text/xml;字符集=utf-8
内容长度:长度
SOAPAction:“http://tempuri.org/checkBarcode"
一串
一串
一串
但安卓KSOAP2发出的信息:

<?xml version="1.0" encoding="utf-8"?>
    <v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
        <v:Header />
        <v:Body>
            <checkBarcode xmlns="http://tempuri.org" id="o0" c:root="1">
                <username i:type="d:string">test</username>
                <password i:type="d:string">test</password>
                <barcode i:type="d:string">2620813000301</barcode>
            </checkBarcode>
        </v:Body>
    </v:Envelope>

测试
测试
2620813000301
使用此代码:

    try {
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        request.addProperty("username", "test");
        request.addProperty("password", "test");
        request.addProperty("barcode", "2620813000301");

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.encodingStyle = "test";

        envelope.setOutputSoapObject(request);

        AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport (URL); 
        androidHttpTransport.debug = true;
        androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");

        androidHttpTransport.call(SOAP_ACTION, envelope);
        Log.d("MyAPP", "----------------- " + androidHttpTransport.requestDump +"\r\n\r\n" + androidHttpTransport.responseDump);
        ((TextView)findViewById(R.id.lblStatus)).setText(androidHttpTransport.requestDump +"\r\n\r\n" + androidHttpTransport.responseDump);
    } catch(Exception E) {
        ((TextView)findViewById(R.id.lblStatus)).setText("ERROR:" + E.getClass().getName() + ": " + E.getMessage());
    }
试试看{
SoapObject请求=新的SoapObject(名称空间、方法名称);
请求。添加属性(“用户名”、“测试”);
请求。添加属性(“密码”、“测试”);
请求。添加属性(“条形码”,“262081300301”);
SoapSerializationEnvelope=新的SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.encodingStyle=“测试”;
envelope.setOutputSoapObject(请求);
AndroidHttpTransport AndroidHttpTransport=新AndroidHttpTransport(URL);
androidHttpTransport.debug=true;
androidHttpTransport.setXmlVersionTag(“”);
调用(SOAP_操作,信封);
Log.d(“MyAPP”、“--------------”+androidHttpTransport.requestDump+“\r\n\r\n”+androidHttpTransport.responseDump);
((TextView)findViewById(R.id.lblStatus)).setText(androidHttpTransport.requestDump+“\R\n\R\n”+androidHttpTransport.responseDump);
}捕获(例外E){
((TextView)findViewById(R.id.lblStatus)).setText(“错误:+E.getClass().getName()+”:“+E.getMessage());
}
我从服务器得到的响应是没有找到任何结果,所以不是错误,但是当我用另一个应用程序或PHP测试它时,它用相同的数据,它说没问题


我认为这是因为当您使用addProperty时,会自动将其添加到soap主体中,所以在您的示例中这是错误的

如果要设置用户名/密码安全标头,必须构建必要的元素[],并在信封上设置为headerOut

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.headerOut = security;
要建立元素[]的安全性,可以使用以下方法

        Element usernameElement = new Element().createElement(OASIS_SECURITY_XSD_URL, "Username");
        usernameElement.addChild(Node.TEXT, username);
        Element passwordElement = new Element().createElement(OASIS_SECURITY_XSD_URL, "Password");
        passwordElement.addChild(Node.TEXT, password);

        Element usernameTokenElement = new Element().createElement(OASIS_SECURITY_XSD_URL, "UsernameToken");
        usernameTokenElement.addChild(Node.ELEMENT, usernameElement);
        usernameTokenElement.addChild(Node.ELEMENT, passwordElement);

        Element securityElement = new Element().createElement(OASIS_SECURITY_XSD_URL, "Security");
        securityElement.setPrefix(null, OASIS_SECURITY_XSD_URL);
        securityElement.addChild(Node.ELEMENT, usernameTokenElement);
在将其设置为headerOut之前,将其全部添加到元素[]中

如何填充“安全”对象(元素[]),以复制AuthHeader?