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.
Related
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.
I have a problem that i dont know how to solve, few days ago i posted a question about using session web service with ksoap. ksoap session web service The problem was that ksoap doesn`t have cookie option so that the sessionID can be stored. But i though about a possible solution, which will work with cookieless session, (sending the sessionID inside the url). The problem is that the session in the web service doesnt work in "cookieless mode", only with cookies which i cant use.
Another idea was to implement the same web service methods inside a aspx page so that the cookieless session will work there, but i dont know how to call a page method.
And since i dont know how to call a page method, i thought that i should call it from my web service and just resend the returned data.
So i need a way to implement my solutions:
1) A way to make cookieless session work in a asmx web service
2) A way to call a page web method with ksoap so that the cookiless session will work there
3) Maybe a way to call an aspx page method from web service method so that i use the web service method like a "bridge" which will resend the page methods returned data.
String NAMESPACE = "http://tempuri.org/";
String METHOD_NAME = "Test";
String SOAP_ACTION = "http://tempuri.org/Test";
String URL = "http://mysite.com/WebService.asmx";
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo pi = new PropertyInfo();
pi.setName("s");
pi.setValue("dddddddd");
pi.setType(String.class);
Request.addProperty(pi);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(Request);
envelope.addMapping(NAMESPACE, "Category",new Category().getClass());
AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject response = (SoapObject)envelope.getResponse();
........................
And my web service method returns a list of Category objects (that is tested and working, but not if i use session).
Does anyone has a proposal??
I was able to do it like so
Basically I'd call login web service and save the Set-Cookie header value that came in the response. Then returning the values in subsequent calls to other web methods.
There will be two 'Set-Cookie' header keys that look something like this:
Set-Cookie: ASP.NET_SessionId=zpe44yb05a105zuz2f24aqi5; path=/;
HttpOnly
Set-Cookie: .ASPXAUTH=4BEC23D4FA4B34C7145550F2FE279E047ED2E16F1503896C949AC3A08DAAC932F6869D9B667A6735D6EA6419074F0F1C34BC3F08210904B2BB9E543CF4CCE6C2E70975F804E2447F60966406ADC1F015D267B0413FF4C79A252C55E904AD86BB315ED161300DF200163CFA77492D680ACCF9C763F3F61F70DA4E8261C9DB6466;
path=/; HttpOnly
Save them both and call subsequent "secure" webmethods with both values concatinated with a ; between them.
The header that you include in subsequent calls should be like this:
Cookie: ASP.NET_SessionId=zpe44yb05a105zuz2f24aqi5; path=/; HttpOnly;
.ASPXAUTH=4BEC23D4FA4B34C7145550F2FE279E047ED2E16F1503896C949AC3A08DAAC932F6869D9B667A6735D6EA6419074F0F1C34BC3F08210904B2BB9E543CF4CCE6C2E70975F804E2447F60966406ADC1F015D267B0413FF4C79A252C55E904AD86BB315ED161300DF200163CFA77492D680ACCF9C763F3F61F70DA4E8261C9DB6466;
path=/; HttpOnly
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.
I'm developing an Android app which needs to connect to a .NET SOAP web service and generate/populate a number of objects from the response. I'm totally new to web services and SOAP, and relatively new to Android. The web service has already been built (not by me).
I've been trying to find info on connecting to SOAP web services in Android. The two basic suggestions seem to be:
Don't use SOAP,
If you do use SOAP, use KSOAP2.
I've looked at various tutorials for KSOAP2 but they all seem to deal only with the most basic, primitive types, such as sending 2 ints to get 1 int back, or sending and receiving strings;, however, I need to send custom objects back and forth.
Is it possible to send and receive custom objects using KSOAP2? If so, how easy/difficult is this? Does the XML have to be parsed to create/populate the objects "by hand"?
EDIT/UPDATE:
After trying for a while to connect to the existing webservice (takes a String and returns a complex object), and getting an error, I decided to try a simpler approach. I connected to a simpler webservice which takes no parameters, and returns an int. This simpler webservice is hosted in the same place as the original one, and the simple one now works fine for me.
FURTHER UPDATE:
All working fine now! Turns out I just had a capitalisation error which was throwing off my parameter.
The final problem was "national" versus "National". Here's the soapUI-generated code:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:GetClientByNationalID>
<!--Optional:-->
<tem:nationalID>XXXXXXX</tem:nationalID>
</tem:GetClientByNationalID>
</soapenv:Body>
</soapenv:Envelope>
And the request code which was being generated by my Java:
<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>
<GetClientByNationalID xmlns="http://tempuri.org/" id="o0" c:root="1">
<NationalID i:type="d:string">
XXXXXXXX
</NationalID>
</GetClientByNationalID>
</v:Body>
</v:Envelope>
My final java code is:
public void webServiceCall() {
String NAMESPACE = "http://tempuri.org/";
String METHOD_NAME = "GetClientByNationalID";
String SOAP_ACTION = "http://tempuri.org/IClientService/GetClientByNationalID";
// unsecure service
String URL = "http://XXXXXXXXXXXXXXXXXXXX.net/FPUnSecureService/ClientService.svc";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
String clientID = "XXXXXXX";
PropertyInfo pi = new PropertyInfo();
pi.setName("nationalID");
pi.setValue(clientID);
pi.setType(clientID.getClass());
request.addProperty(pi);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
envelope.addMapping(NAMESPACE, "GetClientByNationalID", new ClientID().getClass());
Marshal floatMarshal = new MarshalFloat();
floatMarshal.register(envelope);
HttpTransportSE t = new HttpTransportSE(URL);
ClientID client = new ClientID();
t.debug = true;
try {
Log.i("webServiceCall", "Trying call to web service");
t.call(SOAP_ACTION, envelope);
SoapObject response = (SoapObject) envelope.getResponse();
Log.i("success", response.getPropertyAsString(0));
Log.i("success", response.getPropertyAsString(1));
Log.i("success", response.getPropertyAsString(2));
Log.i("success", response.getPropertyAsString(3));
Log.i("success", response.getPropertyAsString(4));
} catch (Exception e) {
Log.e("webServiceCall", "Error calling webservice.");
e.printStackTrace();
System.out.println("Request: " + t.requestDump);
System.out.println("Response: " + t.responseDump);
}
}
I am still confused about these lines:
envelope.addMapping(NAMESPACE, "GetClientByNationalID", new ClientID().
Marshal floatMarshal = new MarshalFloat();
floatMarshal.register(envelope);
I think I added the Marshal parts when I was passing an object as a parameter, I'm not sure if I still need them. I'm also not sure about the addMapping call either, and what I should have in there. Thanks.
Use ksoap2, and check my answers with code examples at the following links: Link1,link2,link3. I wrote some details which will help you understand how to start coding.
let me know if u need help.
UPDATE ( Answering your question about mapping and marshalling)
u use addMapping when you are Sending a complex type ( ie an object) through the soap envelope. How will the webservice recognize the complex type ? u need to create locally a class that implement kvmserializable (as mentionned in my links) which will have same parameter as the object on the server, and then u need to add mapping between it and the class that maps to it on the server, so that the enveloppe when parsed on the server it knows this complex type X map to class X on the server. So if you are not Sending complex type, you don't need to add mapping.(PS:I see that your not sending a complex type since nationalID is of type string. IF lets say nationalID was a complex type of type Z you would do : addMapping(NAMESPACE, Z.class.getSimpleName(), Z.class) )
As for marshalling, it uses java serialization to change Objects to stream of data to be unmarshalled on the web service. So when u are sending a complex type,based on my experience, it is a good practice to add marshalling to change the complex type to streams of data by serializing it. If you find that you don't need it just don't add it, but its always good to understand what are the options out there.
Look at this http://seesharpgears.blogspot.com/2010/10/ksoap-android-web-service-tutorial-with.html , there are examples of how to pass complex objects.
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.