我正在尝试使用Android与KSOAP2一起发布到我自己的测试肥皂服务器(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>
Run Code Online (Sandbox Code Playgroud)
但Android 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>
Run Code Online (Sandbox Code Playgroud)
使用此代码:
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());
}
Run Code Online (Sandbox Code Playgroud)
我从服务器返回的响应是没有找到结果,所以没有错误,但是当我用另一个App或PHP测试它时,它使用相同的数据,它说它没关系.
我认为这是因为
当您使用addProperty时,它会自动将其添加到soap主体中,这样您的样本就会出错.
如果要设置用户名/密码安全标头,则必须构建必要的Element []并在信封上将其设置为headerOut.
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.headerOut = security;
Run Code Online (Sandbox Code Playgroud)
要建立作为Element []的安全性,您可以使用这些内容
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);
Run Code Online (Sandbox Code Playgroud)
并在将其设置为headerOut之前将其全部添加到Element []中