Call WSDL soap method in iPhone - iphone

i am working on an iPhone project in which i have a wsdl which is having description of methods and these methods contains an xml. I need to call these methods which are there in wsdl to get the xml using soap and parse it. I have read so many links and tutorials but was not able to to call the method in the wsdl. Please if u can provide me some links or suggestions.

you can use http://sudzc.com/ ,it gives you ready WSDL
This is a .NET web site solution that uses XSLT to transform SOAP-based web service definition WSDL files into complete code packages. The code that is generated is easy to read and update.
While SudzC supports multiple platforms, it's focus is on code generation for Objective-C libraries for iPhone development.
reference http://code.google.com/p/sudzc/

Please try this code. It worked for me.
[Note: GetDictionary is my method name which is displayed in Web service.and in url please write the url displayed in web service at below portion. It should be matched with url you have written in coding otherwise you will not get output(better copy it from web service)]
-(IBAction)loadData
{
NSString *soapMessage = [NSString stringWithFormat:
#"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
"<SOAP-ENV:Body>\n"
"<GetDictionary></GetDictionary>\n"
"</SOAP-ENV:Body>\n"
"</SOAP-ENV:Envelope>"];
NSURL *url = [NSURL URLWithString:#"http://c70.narolainfotech.local/WCFTest/RestServiceImpl.svc/basic"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [soapMessage length]];
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue: #"urn:RestServiceImpl/GetDictionary" forHTTPHeaderField:#"soapAction"];
// [theRequest addValue: #"urn:RestServiceImpl/getDataBY_ID" 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");
}
}
In connectionDidFinishLoading write following code:
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(#"DONE. Received Bytes: %d", [webData length]);
NSString *strData = [[NSString alloc]initWithData:webData encoding:NSUTF8StringEncoding];
NSLog(#"result : %#",strData);
NSString *result=nil;
NSString *str;
NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
theXML = [theXML stringByReplacingOccurrencesOfString:#"<" withString:#"<"];
theXML = [theXML stringByReplacingOccurrencesOfString:#">" withString:#">"];
theXML = [theXML stringByReplacingOccurrencesOfString:#"&" withString:#"&"];
//NSLog(#"XML from Web-Service ======= \n\n %#",theXML);
NSArray *array=[theXML componentsSeparatedByString:#"<GetDictionaryResult>"];
for(int i=1;i<[array count];i++)
{
str=[array objectAtIndex:i];
NSRange ranfrom=[str rangeOfString:#"</GetDictionaryResult>"];
result =[str substringToIndex:ranfrom.location];
}
NSLog(#"result ::: %#",result);
}
Just Hope that it'll work for you. :)

Related

SOAP XML response reults problems in iPhone

Now i have used SOAP web service in my application and i am not getting the proper xml response results from the server, but the same web service is working fine in the Android side and they are getting the proper SOAP xml response. But it's not working in the iPhone side. So i donno which side having the problem, in the server side or client side(iPhone side only). I have tried ASHTTPRequest also, i am getting the same results.
this is the sample code for calling the web service using NSURL connection,
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"
#"<ViewMemberShipPackagesResponse xmlns=\"http://tempuri.org/\">\n"
#"</ViewMemberShipPackagesResponse>\n"
#"</soap:Body>\n"
#"</soap:Envelope>\n"];
NSString* urlString=[NSString stringWithFormat:#"http://aaaaa.com/aaaaaa.asmx"];
NSURL *url = [NSURL URLWithString:urlString];
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://tempuri.org/ViewMemberShipPackagesResponse" 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 )
{
receiveData = [[NSMutableData data] retain];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSLog(#"didReceiveResponse");
[receiveData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(#"didReceiveData");
[receiveData appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *urlDataString = [[NSString alloc] initWithData:receiveData encoding:NSASCIIStringEncoding];
NSLog(#"the urlDataString is %#", urlDataString);
}
But i am not getting the proper soap xml response from the server. This sample output response from the server,
<?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><ViewMembershipPackagesResponse xmlns="http://tempuri.org/"><ViewMembershipPackagesResult xsi:type="xsd:string">
<?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><ViewMemberShipPackagesResponse xmlns="http://tempuri.org/"><ViewMemberShipPackagesResult>6</ViewMemberShipPackagesResult><Package><ID>5</ID><Name>1 Month Package</Name><DateSpan>1 Month</DateSpan><Price>$29.99</Price><Description>Test</Description></Package><Package><ID>6</ID><Name>1 Month Package with Multiple Locations</Name><DateSpan>1 Month</DateSpan><Price>$89.99</Price><Description>This pacakge to have</Description><
/Package><Package><ID>7</ID><Name>12 Month Test Package</Name><DateSpan>12 Months</DateSpan><Price>$360.00</Price><Description>12 Month Package</Description></Package><Package><ID>4</ID><Name>Andy Test Package</Name><DateSpan>12 Months</DateSpan><Price>$49.99</Price><Description>Test package for a year</Description></Package><Package><ID>2</ID><Name>Golden Package</Name><DateSpan>6 Months</DateSpan><Price>$80.00</Price><Description>There are many variations of passages of Lorem Ipsum available, but the majority have suffered</Description></Package><Package><ID>1</ID><Name>Premium Package</Name><DateSpan>12 Months</DateSpan><Price>$120.00</Price><Description>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.</Description></Package></ViewMemberShipPackagesResponse></soap:Body></soap:Envelope></ViewMembershipPackagesResult></ViewMembershipPackagesResponse> </soap:Body></soap:Envelope>.
Please guide me.
THanks!
change this :
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString * urlDataString = [[NSString alloc] initWithBytes: [receiveData mutableBytes] length:[receiveData length] encoding:NSUTF8StringEncoding];
NSLog(#"the  urlDataString is %#", urlDataString);
}

unable to connect with soap based wcf service from iPhone

I am trying to consume a SOAP web service from my iPhone application but I am getting following error when I check with Charles:
Value cannot be null.
Parameter name: s
Following is my code:
NSString *soapMessage = [NSString stringWithFormat:#"<?xml version=\"1.0\" encoding=\"utf-8\"?><SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Body><updateTimesheet><TimeSheetHourID>222</TimeSheetHourID></updateTimesheet></SOAP-ENV:Body></SOAP-ENV:Envelope>"];
NSURL *url = [NSURL URLWithString:#"http://172.xx.xxx.xx:xxxx/postService.svc/basic"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [soapMessage length]];
NSLog(#"Message Length..%#",msgLength);
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue: #"urn:IpostService/updateTimesheet" forHTTPHeaderField:#"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSHTTPURLResponse* urlResponse = nil;
NSError *error = [[NSError alloc] init];
//NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&urlResponse error:&error];
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(#"Response Code: %d", [urlResponse statusCode]);
if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {
NSLog(#"Response: %#", result);
//here you get the response
}
I am getting error code 500 in response. Can some one please help me?
Thanks
Pankaj
Error 500 is - Internal Server Error. Which means -
The server encountered an unexpected condition which prevented it from fulfilling the request.
Make sure you are sending proper request to your server. Are you able to consume it somewhere else?
the problem is not with your code here .. i advise you to trace the webservice code.

Not receiving data from NSMutableURLRequest (iPhone app sync)

In the past, I created an iPhone app which relies on a sqlite database updated by a php page. When you access the PHP page, it prints out a dump of a sqlite database and the app runs the various queries to update the sqlite database. My previous app worked fine in this manner and would update accordingly. I recently tried to apply the same process to a new app with a much bigger database. I have run into problems.
What I am doing is sending some post variables to a PHP page which recognizes the POST and echoes out the sqlite commands to update the database. The problem is that it takes a few seconds for the page to print the commands. If I have the page print some static text, I get it in my NSLog(#"Response data is going to be ==> %#", result); command. If I have to extract the information from my MySQL database, it takes about 3-5 seconds and then prints (on my normal browser). When I run the app, however, it says that Response data is going to be ==> (null) instead of the commands.
Can anyone shed some light on what might be happening? I know that this works when it doesn't print a lot of stuff and when it is static. The dynamic part makes it return null for some reason.
Help appreciated.
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlBase]];
[request setHTTPMethod:#"POST"];
[request setTimeoutInterval:60.0];
NSData *post = [myParameters dataUsingEncoding: NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d",[post length]];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Current-Type"];
[request setHTTPBody:post];
[request setHTTPBody:[myParameters dataUsingEncoding:NSUTF8StringEncoding]];
// set headers
NSString *contentType = [NSString stringWithFormat:#"text/plain"];
NSHTTPURLResponse *urlResponse = nil;
NSError *error = [[NSError alloc] init];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request
returningResponse:&urlResponse
error:&error];
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(#"Response data is going to be ==> %#", result);
try to understand this code..it helps u.
NSString *soapMessage=[NSString stringWithFormat:#" YOUR WEB SERVICE CODE",myTxt1.text,myTxt2.text];
NSLog(#"%#",soapMessage);
//-----------create connection
NSURL *url=[NSURL URLWithString:#"http://your local host webservice url"];
NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:url];
NSString *msgLength=[NSString stringWithFormat:#"%d",[soapMessage length]];
//----------sending request
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue: #"http://tempuri.org/AutenticateUser" forHTTPHeaderField:#"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection=[[NSURLConnection alloc]initWithRequest:theRequest delegate:self];
//[activityIndicator startAnimating];
if ( theConnection )
{
webData = [[NSMutableData data] retain];
}
else
{
NSLog(#"theConnection is NULL");
}

Base 64 encoding random behaviour UIImage

I am sending an image to the server by converting the NSData object into an NSString object using Base64 scheme by using....
NSData *imgData=UIImagePNGRepresentation([objCP userImage]);
NSString *encodedString=[Base64Coder encodeData:imgData];
I am observing a random behavior....sometimes I get "==" in the encoded string at the end of the string and image does not uploaded.There may be some other characters also in between the string.If I do not get these characters at the end...image gets uploaded.
To overcome this...I also used this method to convert these characters into valid(assumed to be accepted)::
-(NSString *)urlEncodedVersion:(NSString *)strString
{
NSMutableString *strTemp = [[NSMutableString alloc] initWithFormat:#"%#",strString] ;
NSArray *escapeChars = [NSArray arrayWithObjects:#";",#"?",#":",#"#", #"&",#"=",#"+",#"$",#",", #"[",#"]",#"#",#"!",#"’",#"(", #")",#"*",#" ",nil];
NSArray *replaceChars = [NSArray arrayWithObjects: #"%3B",#"%3F",#"%3A",
#"%40",#"%26",#"%3D", #"%2B",#"%24",#"%2C",#"%5B",#"%5D", #"%23",#"%21",#"%27", #"%28",#"%29",#"%2A",#"%20",nil];
//NSMutableString *tempStr = [[self mutableCopy] autorelease];
for(int i = 0; i < [escapeChars count]; i++)
{
[strTemp replaceOccurrencesOfString:[escapeChars objectAtIndex:i] withString:[replaceChars objectAtIndex:i] options:NSLiteralSearch range:NSMakeRange(0,[strTemp length])];
}
return strTemp;
}
but it is also not serving.
This is my whole post body::
-(void)uploadProfileInfo:(CreateProfile *)objCP
{
NSData *imgData=UIImagePNGRepresentation([objCP userImage]);
NSString *encodedString=[Base64Coder encodeData:imgData];
NSString *refinedString=[self urlEncodedVersion:encodedString];
NSString *soapMessage = [NSString stringWithFormat:
#"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<soapenv:Envelope \n"
"xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" \n"
"xmlns:tem=\"http://tempuri.org/\" \n"
"xmlns:wcf=\"http://schemas.datacontract.org/2004/07/NextToMe_BusinessEntity\"> \n"
"<soapenv:Header/>\n"
"<soapenv:Body>\n"
"<tem:CreateProfile>\n"
"<tem:objUser>\n"
"<wcf:Email>%#</wcf:Email>\n"
"<wcf:Mode>%#</wcf:Mode> \n"
"<wcf:Name>%#</wcf:Name>\n"
"<wcf:ProfileImage>%#</wcf:ProfileImage>\n"
"<wcf:RequestDateTime>%#</wcf:RequestDateTime>\n"
"<wcf:Status>%#</wcf:Status>\n"
"<wcf:StatusSpecified>%#</wcf:StatusSpecified>\n"
"<wcf:UDID>%#</wcf:UDID>\n"
"</tem:objUser>\n"
"</tem:CreateProfile>\n"
"</soapenv:Body>\n"
"</soapenv:Envelope>\n",[objCP email],[objCP mode],[objCP name],encodedString,#"",[objCP status],[objCP statusSpecified],[objCP UDID]];
NSURL *url = [NSURL URLWithString:kBaseURL];
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://tempuri.org/XXXX/CreateProfile" 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(#"The Connection is NULL");
}
}
The = characters you see at the end are padding. This is according to RFC4648 3.2. Padding of Encoded Data. The other end should be able to digest that. Apparently that is not happening. You should compare the behavior with a third party library to check whether the encoding or decoding is wrong. If you can make it work removing the padding, then good for you, but that's a fault in the decoding library.

Just want to confirm if this request is 100% secure connection

I am writing an iPhone App and I need to securely send an XML request and receive it ALL securely. I think I did it right I just want some confirmation that the below code is all secure.
NSURL *url = [NSURL URLWithString: #"https://secure.site.com/request"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [xml length]];
[req addValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[req addValue:msgLength forHTTPHeaderField:#"Content-Length"];
[req setHTTPMethod:#"POST"];
[req setHTTPBody: [xml dataUsingEncoding:NSUTF8StringEncoding]];
conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (conn)
webData = [[NSMutableData data] retain];
and in the connectionDidFinishLoading..
- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *gotXml = [[NSString alloc]
initWithBytes: [webData mutableBytes]
length:[webData length]
encoding:NSUTF8StringEncoding];
Yes, so long as the URL has https:// in it, the request will be sent securely.