I'm trying to develop an app that uses data from a SOAP service. is the first time I use a SOAP service. After reading several tutorials I thought I would be able to set the request to the service in the right way but I always get errors in the response. What's wrong ?!
this is the code for the request
NSString *soapMessage = [NSString stringWithFormat:
#"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"http://microsoft.com/webservices/\">\n"
"<SOAP-ENV:Body>\n"
"<ns1:ListaComuni>\n"
"<Nazione>italia</Nazione>\n"
"</ns1:ListaComuni>\n"
"</SOAP-ENV:Body>\n"
"</SOAP-ENV:Envelope>\n"
];
NSURL *url = [NSURL URLWithString:#"http://www.xxxxx.xxx/XXXXX.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [soapMessage length]];
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
When i receive the answer
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(#"DONE. Received Bytes: %d", [webData length]);
NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
NSLog(#"RISPOSTA %#",theXML);
}
The log:
RISPOSTA <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><ListaComuniResponse xmlns="http://microsoft.com/webservices/"><ListaComuniResult><anyType xsi:type="xsd:string">Object reference not set to an instance of an object.</anyType></ListaComuniResult></ListaComuniResponse></soap:Body></soap:Envelope>
I tested the XML request on http://wsdlbrowser.com and it all works.
What's wrong ?!
Thanks
Ok...
i found the error...the correct request is:
NSString *soapMessage = [NSString stringWithFormat:
#"<?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:Body>"
"<ListaComuni xmlns=\"http://microsoft.com/webservices/\">"
"<Nazione>%#</Nazione>\n"
"</ListaComuni>"
"</soap:Body>"
"</soap:Envelope>", nameInput.text
];
Related
I am first time using soap service,
This is the data server expecting,
POST /updateLocation.asmx HTTP/1.1
Host: www.geoming.com
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<setLocation xmlns="http://geoming.com/">
<pLati>string</pLati>
<pLongi>string</pLongi>
<pStatusMsg>string</pStatusMsg>
<pID>string</pID>
<pSpeed>string</pSpeed>
<pStreet1>string</pStreet1>
<pStreet2>string</pStreet2>
<pCity>string</pCity>
<pState>string</pState>
<pCountry>string</pCountry>
<pDate>string</pDate>
<pTimeZone>string</pTimeZone>
</setLocation>
</soap12:Body>
</soap12:Envelope>
and in objective c section i am doing like this,
soapMessage = [NSString stringWithFormat:#"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
"<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope/\">"
"<soap12:Body>"
"<setLocation xmlns=\"http://www.geoming.com/\">"
"<pLati>-33.868</pLati>"
"<pLongi>151.2086</pLongi>"
"<pStatusMsg>santanu test</pStatusMsg>"
"<pID>3</pID>"
"<pSpeed>12</pSpeed>"
"<pStreet1>CD - 96</pStreet1>"
"<pStreet2>Salt lake city</pStreet2>"
"<pCity>kolkata</pCity>"
"<pState>west bengal</pState>"
"<pCountry>india</pCountry>"
"<pDate>16-04-2015</pDate>"
"<pTimeZone>-5.30</pTimeZone>"
"</setLocation>"
"</soap12:Body>"
"</soap12:Envelope>"];
//Now create a request to the URL
NSURL *url = [NSURL URLWithString:#"http://www.geoming.com/updateLocation.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [soapMessage length]];
//ad required headers to the request
[theRequest addValue:#"www.geoming.com" forHTTPHeaderField:#"Host"];
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue: #"http://www.geoming.com/setLocation" forHTTPHeaderField:#"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
It's always showing "System.Web.Services.Protocols.SoapException: Server did not recognize the value of HTTP Header SOAPAction: http://www.geoming.com/setLocation. System.Web.Services.Protocols.Soap11ServerProtocolHelper.RouteRequest()".
Unable to to find the solution as i am new in ios.
Wrong SOAPAction. Instead #"http://www.geoming.com/setLocation" you need use #"http://geoming.com/setLocation"
Try my code:
static NSString* const cstrSetLocMsg = #"<?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:Body>"
"<setLocation xmlns=\"http://geoming.com/\">"
"<pLati>-33.868</pLati>"
"<pLongi>151.2086</pLongi>"
"<pStatusMsg>santanu test</pStatusMsg>"
"<pID>3</pID>"
"<pSpeed>12</pSpeed>"
"<pStreet1>CD - 96</pStreet1>"
"<pStreet2>Salt lake city</pStreet2>"
"<pCity>kolkata</pCity>"
"<pState>west bengal</pState>"
"<pCountry>india</pCountry>"
"<pDate>16-04-2015</pDate>"
"<pTimeZone>-5.30</pTimeZone>"
"</setLocation>"
"</soap:Body>"
"</soap:Envelope>";
And create request:
+ (NSMutableURLRequest*) requestSetLoc{
NSString *_soapMsg = cstrSetLocMsg;
NSURL *url = [NSURL URLWithString:#"http://www.geoming.com/updateLocation.asmx"];
NSMutableURLRequest *req = [[[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.f] autorelease];
NSString *msgLength = [NSString stringWithFormat:#"%d", [_soapMsg length]];
[req addValue:#"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[req addValue:#"http://geoming.com/setLocation" forHTTPHeaderField:#"SOAPAction"];
[req addValue:msgLength forHTTPHeaderField:#"Content-Length"];
[req setHTTPMethod:#"POST"];
[req setHTTPBody: [_soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
return req;}
It should works!
Change your string to
soapMessage = [NSString stringWithFormat:#"<?xml version=\"1.0\" encoding=\"utf-8\"?>\
<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope/\">\
<soap12:Body>\
<setLocation xmlns=\"http://www.geoming.com/\">\
<pLati>-33.868</pLati>\
<pLongi>151.2086</pLongi>\
<pStatusMsg>santanu test</pStatusMsg>\
<pID>3</pID>\
<pSpeed>12</pSpeed>\
<pStreet1>CD - 96</pStreet1>\
<pStreet2>Salt lake city</pStreet2>
<pCity>kolkata</pCity>\
<pState>west bengal</pState>\
<pCountry>india</pCountry>\
<pDate>16-04-2015</pDate>\
<pTimeZone>-5.30</pTimeZone>\
</setLocation>
</soap12:Body>\
</soap12:Envelope>"];
I have implemented SOAP Service in iOS.
I am using the following code for sending the request.
NSString *sSOAPMessage = #"<?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:Body>"
"<CelsiusToFahrenheit xmlns=\"http://www.w3schools.com/webservices/\">"
"<Celsius>50</Celsius>"
"</CelsiusToFahrenheit>"
"</soap:Body>"
"</soap:Envelope>";
NSURL *sRequestURL = [NSURL URLWithString:#"http://w3schools.com/webservices/tempconvert.asmx"];
NSMutableURLRequest *myRequest = [NSMutableURLRequest requestWithURL:sRequestURL];
NSString *sMessageLength = [NSString stringWithFormat:#"%d", [sSOAPMessage length]];
[myRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[myRequest addValue: #"http://www.w3schools.com/webservices/CelsiusToFahrenheit" forHTTPHeaderField:#"SOAPAction"];
[myRequest addValue: sMessageLength forHTTPHeaderField:#"Content-Length"];
[myRequest setHTTPMethod:#"POST"];
[myRequest setHTTPBody: [sSOAPMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:myRequest delegate:self];
if( theConnection ) {
webData = [[NSMutableData data] retain];
}else {
NSLog(#"Some error occurred in Connection");
}
And I am getting following response. Not getting any value i.e. Celsius or Fahrenheit value in response.
Check it works now:
NSString *soapMessage = [NSString stringWithFormat:
#"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<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/\">\n"
"<soap:Body>\n"
"<CelsiusToFahrenheit xmlns=\"http://www.w3schools.com/webservices/\"> <Celsius>25.5</Celsius> </CelsiusToFahrenheit>"
"</soap:Body>\n"
"</soap:Envelope>\n"
];
NSLog(soapMessage);
NSURL *url = [NSURL URLWithString:#"http://www.w3schools.com/webservices/tempconvert.asmx?op=CelsiusToFahrenheit"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [soapMessage length]];
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue: #"http://www.w3schools.com/webservices/CelsiusToFahrenheit" forHTTPHeaderField:#"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if( theConnection )
{
webData = [[NSMutableData data] retain];
}
else
{
NSLog(#"theConnection is NULL");
}
Then get the response as:
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *data=[[NSString alloc]initWithFormat:#"%#",webData ];
// NSLog(#"DONE. Received Bytes: %d, \n %#", [webData length],data);
NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
NSLog(#"Response is: %#",theXML);
}
Afterwords you need to parse it.
i am facing a problem while requesting server in SOAP format. when i am pasting the Soap envelope in the the browser(just for testing the web services) then it gives correct response. But when i am requesting from app then it give me irrelevant response.
I searched a lot on but every where i found the same technique for soap request.
Here is my code what i am using.
NSString *soapMessage = [NSString stringWithFormat:
#"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<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/\">\n"
"<soap:Body>\n"
"<ProfileLogin xmlns=\"http://tempuri.org/\">"
"<strLoginRequest>"
"<email>abc#gmail.com.au</email>"
"<password>123456</password>"
"</strLoginRequest>"
" </ProfileLogin>"
"</soap:Body>"
"</soap:Envelope>"
];
NSLog(soapMessage);
NSURL *url = [NSURL URLWithString:#"http://www.abc.com.au/mobileapp.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [soapMessage length]];
[theRequest setValue:#"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest setValue:msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setValue:#"http://tempuri.org/ProfileLogin" forHTTPHeaderField:#"SOAPAction"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if( theConnection )
{
webData = [[NSMutableData data] retain];
}
else
{
NSLog(#"theConnection is NULL");
}
and getting response
<ProfileReply><Success>False</Success><Error>Data at the root level is invalid. Line 1, position 1.</Error></ProfileReply>
please some buddy let me know it is my fault of mycode or from server side.
Try
[theRequest setValue:#"application/soap+xml;charset = utf-8" forHTTPHeaderField:#"Content-Type"];
try this
[theRequest setValue:#"ProfileLogin" forHTTPHeaderField:#"SOAPAction"];
instead of
[theRequest setValue:#"http://tempuri.org/ProfileLogin" forHTTPHeaderField:#"SOAPAction"];
I am accessing xml web service in my iPhone application. But i am not able to get the appropriate data from the server. I am getting following error in connectionDidFinishLoading delegate method of NSURLConnection.
soap:ServerServer
was unable to process request. ---> Object reference not set to an
instance of an object.
Following is my code :
NSString *deviceID = [OpenUDID value];
NSString *soapMsg = [NSString stringWithFormat:
#"<?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>admin</Username>"
"<Password>admin</Password>"
"</AuthHeader>"
"</soap:Header>"
"<soap:Body>"
"<WsGetAllDrugDetailsXML xmlns=\"http://tempuri.org/\">"
"<UserId>%d</UserId>"
"<IMEI>%#</IMEI>"
"</WsGetAllDrugDetailsXML>"
"</soap:Body>"
"</soap:Envelope>",[Appdelegate.UserID intValue],deviceID];
//---print it to the Debugger Console for verification---
NSLog(#"%#", soapMsg);
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:Appdelegate.wsURL];
//---set the various headers---
NSString *msgLength = [NSString stringWithFormat:#"%d", [soapMsg length]];
[req addValue:#"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[req addValue:#"http://tempuri.org/WsGetAllDrugDetailsXML" forHTTPHeaderField:#"SOAPAction"];
[req addValue:msgLength forHTTPHeaderField:#"Content-Length"];
//---set the HTTP method and body---
[req setHTTPMethod:#"POST"];
[req setHTTPBody:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (conn) {
webData = [[NSMutableData data] retain];
}
I'm not able to predict what is going wrong from my side. Or it is the error from server side that i'm not receiving the data.
Please help me.Thanks in Advance!
Your Content-Length value is not correct:
The length must be in bytes. NSString's length method returns the number of UTF-16 code units.
You may fix it as follows:
NSData* bodyData = [soapMsg dataUsingEncoding:NSUTF8StringEncoding];
[req addValue:[NSString stringWithFormat:#"%d",(int)[bodyData length]] forHTTPHeaderField:#"Content-Length"];
[req setHTTPBody:bodyData];
I have written a function to get a response from web service. But now I want to send parameters to a web service. How can I modify the code below to send parameters to another web service? I want to work with the below code, I am new to IOS and dont want to mess up since I have a working code.
- (IBAction)buttonClick:(id)sender {
recordResults = FALSE;
NSString *soapMessage = [NSString stringWithFormat:
#"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<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/\">\n"
"<soap:Body>\n"
"<GetUserList xmlns=\"http://methodoor.com/checkupservice/\" />\n"
"</soap:Body>\n"
"</soap:Envelope>\n"];
//NSLog(soapMessage);
_lbl_result.text = soapMessage;
NSURL *url = [NSURL URLWithString:#"http://servicing2.rotanet.com.tr/service.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [soapMessage length]];
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue: #"http://methodoor.com/checkupservice/GetUserList" forHTTPHeaderField:#"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if( theConnection )
{
webData = [[NSMutableData data] retain];
}
else
{
NSLog(#"theConnection is NULL");
}
//[nameInput resignFirstResponder];
}
I see you are accessing SOAP web services. And you are already sending data in to your web service in SOAP message.
As this is a SOAP API, you only need to change the SOAP Action [theRequest addValue: #"http://methodoor.com/checkupservice/GetUserList" forHTTPHeaderField:#"SOAPAction"]; and SOAP message, which you can get from the WSDL or SVC file, to access another method of your web service.
For more info you can have a look at this link. Hope this helps.