Ksoap2 complex request - android

Need help with converting soap request to ksoap 2 request for android. Soap request looks like this -
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:tim="http://www.g.com/schemas/T"
xmlns:tim1="http://www.g.com/schemas/T.Entities"
xmlns:f="http://www.g.com/schemas/T.Filters"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header/>
<soapenv:Body>
<tim:GetFilteredList>
<tim:entityName>User</tim:entityName>
<tim:filter i:type="f:UserFilter">
<tim1:Id>0</tim1:Id>
<f:Login>login1</f:Login>
</tim:filter>
</tim:GetFilteredList>
</soapenv:Body>
</soapenv:Envelope>
any ideas?

Related

Android Ksoap2 xml request generaion

I need to know how to create soap request in Android its hierarchy for the xml request given below,facing problem creating the SOAP request.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cus="http://siebel.com/CustomUI" xmlns:upb="http://www.siebel.com/xml/UPBGAttachmentQueryIO">
<soapenv:Header>
<UsernameToken xmlns="http://siebel.com/webservices">vadmin</UsernameToken>
<PasswordText xmlns="http://siebel.com/webservices">vadmin</PasswordText>
<SessionType xmlns="http://siebel.com/webservices">None</SessionType>
</soapenv:Header>
<soapenv:Body>
<cus:UpsertAttachment_Input>
<cus:Process_spcInstance_spcId></cus:Process_spcInstance_spcId>
<cus:Siebel_spcOperation_spcObject_spcId>?</cus:Siebel_spcOperation_spcObject_spcId>
<upb:ListOfUPBGAttachmentQueryIO>
<upb:Upbgsrupdateattachmentbc>
<upb:Id>0</upb:Id>
<upb:ActivityComments>Test2</upb:ActivityComments>
<upb:ActivityFileExt>jpg</upb:ActivityFileExt>
<upb:ActivityFileName>SeeTest3</upb:ActivityFileName>
<upb:SrNum>17110700001</upb:SrNum>
<upb:ActivityFileBuffer >ecnoded64bit string </upb:ActivityFileBuffer>
</upb:Upbgsrupdateattachmentbc>
</upb:ListOfUPBGAttachmentQueryIO>
</cus:UpsertAttachment_Input>
</soapenv:Body>
</soapenv:Envelope>
Quick response would be appreciated.Thanks

how to create Soap request with token and envelope

I am trying to create soap request in android but not able to get how to generate this.
I have gone through following URL:-
url-1
url-2
but not able to understand how to create soap request like this.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:stal="http://ws.soapwebserv.com/Info">
<soapenv:Header>
<wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis- open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken wsu:Id="UsernameToken-30" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsse:Username>alias101#soapwebserv.com</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">somesecurepassword</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
<soapenv:Body>
<stal:InfoRequest>
<token>randomtokenrandomtoken</token>
</stal:InfoRequest>
</soapenv:Body>
</soapenv:Envelope>
please help me out guys... I am new to Soap. :(
simple way is to copy the entire soapenvelope in raw folder as xml file and pass your username ,password and token with help of pattern matching . Now you have the entire request in the form of string pass the request to httppost method.let me know that helps your issue.

Getting wrong response from ksoap2 on android

I'm using ksoap2 library on android to connect to a web service. I run the following code:
SoapObject request = new SoapObject(NAMESPACE, method);
request.addProperty("UserName", username);
request.addProperty("TransactionID", id);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
nvelope.setOutputSoapObject(request);
HttpTransportSE ht = new HttpTransportSE(URL);
ht.call(NAMESPACE + method, envelope);
As you can see above, there are two parameters. Here's the wsdl file for this part:
<s:element minOccurs="1" maxOccurs="1" name="TransactionID" type="s:int" />
<s:element minOccurs="0" maxOccurs="1" name="UserName" type="s:string" />
Problem is that I get different responses from the time I call it with same parameters from SoapUI. Here's how SoapUI request looks like:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="http://whatever.com/Webservice/Request.asmx">
<soapenv:Header/>
<soapenv:Body>
<req:GetProduct>
<req:TransactionID>id</req:TransactionID>
<!--Optional:-->
<req:UserName>username</req:UserName>
</req:GetProduct>
Parameters name have an extra :req prefix that may cause my problem. When I add the req: to my java parameters name, an exception occurs:
java.io.IOException: Server returned HTTP response code: 400 for URL: http://whatever.com/WebService/Request.asmx?wsdl/
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1345)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1339)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:993)
at org.ksoap2.transport.ServiceConnectionSE.openInputStream(ServiceConnectionSE.java:113)
at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:184)
at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:96)
at SoapCommand.invokeMethod(SoapCommand.java:42)
at SoapCommand.main(SoapCommand.java:16)
Caused by: java.io.IOException: Server returned HTTP response code: 400 for URL: http://whatever.com/WebService/Request.asmx?wsdl/
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1290)
at sun.net.www.protocol.http.HttpURLConnection.getHeaderFields(HttpURLConnection.java:2129)
at org.ksoap2.transport.ServiceConnectionSE.getResponseProperties(ServiceConnectionSE.java:84)
at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:167)
... 3 more
Exception in thread "main" java.lang.NullPointerException
at SoapCommand.main(SoapCommand.java:17)
It's driving me crazy.
There is nothing wrong with your code. The req prefix is as per SOAP/XML standards. The problem lies in the way the webservice is deployed and the way ksoap works.
All webservices are expected to respond back with the WSDL if you append ?wsdl to the webservice endpoint. ksoap expects this and requests the WSDL. In this case, the webservice doesn't send back the WSDL, throwing a 400 error.
You should ask the webservice provider why the WSDL doesn't show up when you send a request for http://whatever.com/WebService/Request.asmx?wsdl.
If that doesn't work out, consider creating a SOAP client on a server, connecting to the webservice, and call your server with a REST webservice from Android. That way, you use less bandwidth and do less processing on the device.

Sending complex type in ksoap request to Wcf web service

I am trying to connect to a Wcf web serice from android client.My client is working fine with simple data types but when i am trying to call with complex data it is giving issues.
I have done this:
Credentials credentials=new Credentials();
credentials.Username="xyz";
credentials.Password="123";
PropertyInfo info=new PropertyInfo();
info.setName("credentials");
info.setValue(credentials);
info.setNamespace(NAMESPACE);
info.setType(new Credentials().getClass());
request.addProperty(info);
SoapSerializationEnvelope envelope =
new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransport = new HttpTransportSE(URL);
Object response=null;
httpTransport.call(SOAP_ACTION, envelope);
response = envelope.getResponse();
Here whenever i send Credentials without doing the info.setValue(credentials) i am able to send the request to the server but the username and password field blank.
In case i add this info.setValue(credentials) i get eserilization error. I have wasted some 2-3 days on this please help.
I got the solution for my question.I noticed the request generated in java and android client.
Java client
<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body><ns4:myfunction xmlns:ns4="http://tempuri.org/">
<ns4:newMobile>91----------</ns4:newMobile>
<ns4:countryId>1</ns4:countryId>
<ns4:credentials>
<ns2:Password xmlns:ns2="http://schemas.datacontract.org/2004/07/mycompanyWebService.Models">123</ns2:Password>
<ns2:Username xmlns:ns2="http://schemas.datacontract.org/2004/07/mycompanyWebService.Models">xyz</ns2:Username></ns4:credentials>
</ns4:myfunction>
</soapenv:Body>
</soapenv:Envelope>
Android Client
<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>
<myfunction xmlns="http://tempuri.org/" id="o0" c:root="1">
<Mobile i:type="d:string">91----------</Mobile>
<countryId i:type="d:string">1</countryId>
<credentials i:type="d:anyType">
<Username i:type="d:string">hm.1</Username>
<Password i:type="d:string">123</Password></credentials>
</myfunction>
</v:Body>
</v:Envelope>
In case of java client it was working but not in android so I used marshaling in order to change the start tag and end tag and it worked for me.

Authentication in KSOAP2 request to .NET webservice in Android

I am trying to access a secure .NET SOAP webservice in an Android app, using the KSOAP2 library. I have checked for other answers on Stack Overflow, but so far none of them have helped me.
A username and password are required for authentication. The method also takes a String and returns a complex type. I have gotten this to work with an identical but unsecure service (ie, no username/password, but passing String and returning complex type works fine).
I can connect to the secure webservice using soapUI. All I have to do is give it the correct String parameter, set the username and password through the SoapUI request properties, and also set the WSS-PasswordType to "PasswordText".
I am confused about how to achieve this through my Java code. The SoapUI request (which works) looks like this:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header><wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken wsu:Id="UsernameToken-1" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsse:Username>(my username) </wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">(my password) </wsse:Password>
<wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">eic5EdyTomcBLocwyph5Mw==</wsse:Nonce>
<wsu:Created>2012-02-08T09:47:55.225Z</wsu:Created>
</wsse:UsernameToken></wsse:Security>
</soapenv:Header>
<soapenv:Body>
<tem:GetClientByNationalID>
<!--Optional:-->
<tem:nationalID>(the nationalID) </tem:nationalID>
</tem:GetClientByNationalID>
</soapenv:Body>
</soapenv:Envelope>
I figured I should be passing username and password in the Soap header then so I tried following this answer but this didn't work. I then used [Lalit's answer] (SOAP web service on android) below to edit the header of my request to try to make it identical to the SoapUI-generated request, but I am still getting the same error. Here is what my code is now producing:
<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>
<n0:Security mustUnderstand="1" xmlns:n0="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<n0:UsernameToken n1:Id="UsernameToken-1" xmlns:n1="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<n0:Username>(my username)</n0:Username>
<n0:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">(mypassword)</n0:Password>
<n0:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">eic5EdyTomcBLocwyph5Mw==</n0:Nonce>
<wsu:Created>2012-02-08T09:47:55.225Z</wsu:Created>
</n0:UsernameToken>
</n0:Security>
</v:Header>
<v:Body>
<GetClientByNationalID xmlns="http://tempuri.org/" id="o0" c:root="1">
<nationalID i:type="d:string">(testnationalID)</nationalID>
</GetClientByNationalID>
</v:Body>
This is the error I'm still getting in LogCat:
02-10 10:53:49.261: W/System.err(1187): javax.net.ssl.SSLHandshakeException:
java.security.cert.CertPathValidatorException: Trust anchor for certification path
not found.
This makes it look like a certificate issue, so I imported the cert from the server with InstallCert and verified with Keytool that it's in my keystore. This made no difference. On the other hand, I assume it's not a certificate issue since SoapUI worked before I had that cert installed.
Now I'm confused, so here are my questions:
1) Is adding the cert to the cacerts keystore in my JRE lib/security enough, or do I need to write some Java code to then access this cert?
2) Is this a cert issue at all, if SoapUI was able to connect successfully to the webservice without me installing the cert?
3) If it's not a cert issue, can anyone advise on what kind of code I need to use to produce a Soap request that will successfully authenticate and then execute the method call? I presume this is a case of getting my java-produced request to look as similar as possible to the SoapUI-generated request.
You can create the Header element and add the Authentication details,
Element[] header = new Element[1];
header[0] = new Element().createElement(NAMESPACE,"AuthHeader");
Add Username tag with username,
Element username = new Element().createElement(NAMESPACE, "username_tag");
username.addChild(Node.TEXT, "username_value");
header[0].addChild(Node.ELEMENT, username);
Add Password tag with password,
Element password = new Element().createElement(NAMESPACE, "password_tag");
password.addChild(Node.TEXT, "password_value");
header[0].addChild(Node.ELEMENT, password);
Add the header to the SoapSerializationEnvelope
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope
.VER11);
envelope.dotNet = true;
envelope.headerOut = header;
envelope.setOutputSoapObject(request);
I hope it is not too late. Please have a look at the answer on the following link.
https://stackoverflow.com/a/17691465/2499764
I can share the code for constructing the header if you are still interested.

Categories

Resources