KSoap2 Android的不是有效的SOAP [英] KSoap2 Android not valid SOAP
问题描述
我想张贴到我自己的测试SOAP服务器(C#)与Android结合KSOAP2。
现在我已经从SOAP服务器的规格,它预计:
POST /SharingpointCheckBar$c$c.asmx HTTP / 1.1
主持人:awc.test.trin-it.nl
内容类型:text / xml的;字符集= UTF-8
内容长度:长度
SOAPAction报:http://tempuri.org/checkBar$c$c
< XML版本=1.0编码=UTF-8&GT?;
<肥皂:信封的xmlns:XSI =http://www.w3.org/2001/XMLSchema-instance的xmlns:XSD =http://www.w3.org/2001/XMLSchema的xmlns:肥皂= http://schemas.xmlsoap.org/soap/envelope/">
<肥皂:头>
< AuthHeader的xmlns =http://tempuri.org/>
<用户名>字符串< /用户名>
<密码>字符串< /密码>
< / AuthHeader>
< / SOAP:头>
<肥皂:身体与GT;
< checkBar code的xmlns =http://tempuri.org/>
<酒吧code>字符串< /条code取代;
< / checkBar code取代;
< / SOAP:身体与GT;
< /肥皂:信封>
但Android的KSOAP2送出:
< XML版本=1.0编码=UTF-8&GT?;
<五:信封的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/>
<五:页眉/>
<五:身体与GT;
< checkBar code的xmlns =http://tempuri.orgID =O0C:根=1>
<用户名我:TYPE =D:字符串>测试< /用户名>
<密码我:TYPE =D:字符串>测试< /密码>
<酒吧codeI:TYPE =D:字符串> 2620813000301< /条code取代;
< / checkBar code取代;
< / V:车身>
< / V:信封>
通过这个code:
尝试{
SoapObject请求=新SoapObject(命名空间METHOD_NAME);
request.addProperty(用户名,测试);
request.addProperty(密码,测试);
request.addProperty(栏code,2620813000301);
SoapSerializationEnvelope包=新SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = TRUE;
envelope.encodingStyle =测试;
envelope.setOutputSoapObject(要求);
AndroidHttpTransport androidHttpTransport =新AndroidHttpTransport(URL);
androidHttpTransport.debug = TRUE;
androidHttpTransport.setXmlVersionTag(&所述;?xml的版本= \1.0 \编码= \utf-8的\>?);
androidHttpTransport.call(SOAP_ACTION,包);
Log.d(MyApp的,-----------------+ androidHttpTransport.requestDump +\ r \ñ\ r \ N+ androidHttpTransport.responseDump);
((TextView中)findViewById(R.id.lblStatus))的setText(androidHttpTransport.requestDump +\ r \ñ\ r \ N+ androidHttpTransport.responseDump)。
}赶上(例外五){
((TextView的)findViewById(R.id.lblStatus))的setText(错误:+ E.getClass()的getName()+:+ E.getMessage());
}
我回来从服务器的响应是,有没有结果发现,所以不是一个错误,但是当我与其他应用程序或PHP测试了一下,用同样的数据,它说,它的确定。
我想这是因为
当您使用方法addProperty这会自动将其添加到SOAP体,因此是错误的样本。
如果你想设置一个用户名/密码的安全性的头部,你必须建立必要的元素[]和作为headerOut在信封上注明设置。
SoapSerializationEnvelope包=新SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.headerOut =安全;
要建立安全性的元素[您使用的这些方针的东西]
元素usernameElement =新元素()的createElement(OASIS_SECURITY_XSD_URL,用户名)。
usernameElement.addChild(Node.TEXT,用户名);
。元素passwordElement =新元素()的createElement(OASIS_SECURITY_XSD_URL,密码);
passwordElement.addChild(Node.TEXT,密码);
。元素usernameTokenElement =新元素()的createElement(OASIS_SECURITY_XSD_URL,用户名令牌);
usernameTokenElement.addChild(Node.ELEMENT,usernameElement);
usernameTokenElement.addChild(Node.ELEMENT,passwordElement);
。元素securityElement =新元素()的createElement(OASIS_SECURITY_XSD_URL,安全);
securityElement.set preFIX(空,OASIS_SECURITY_XSD_URL);
securityElement.addChild(Node.ELEMENT,usernameTokenElement);
和之前将其设置为headerOut说起来中的一个元素[]
I am trying to post to my own test soap server (C#) with Android in combination with KSOAP2.
Now I have the specifications from the SOAP server, it expects:
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>
But what Android KSOAP2 sends out:
<?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>
With this code:
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());
}
The response I get back from the server is that there are no results found, so not an error, but when I test it with another App or PHP, it with the same data, it says it's OK.
I think it's because of the
When you use addProperty this automatically adds it to the soap body, so thats wrong in your sample.
If you want to set up a username/password security header you have to build up the necessary Element[] and set it with as headerOut on your envelope.
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.headerOut = security;
To build up security as an Element[] you use something along these lines
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);
and add it all up in an Element[] before you set it to headerOut
这篇关于KSoap2 Android的不是有效的SOAP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!