unable to connect with soap based wcf service from iPhone - 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.

Related

Call WSDL soap method in 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. :)

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");
}

post xmldata through webservice iphone problem

This is my xml
NSString *Message = [NSString stringWithFormat:
#"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<query>\n"
"<IdCategory>1088</IdCategory>\n"
"</query>\n"];
NSURL *url = [NSURL URLWithString:#"http://bvd.vndsupport.com/_ws/api/categories/index.asp"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [Message length]];
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [Message dataUsingEncoding:NSUTF8StringEncoding]];
NSString *returnString = [[NSString alloc] initWithData:[NSURLConnection sendSynchronousRequest:theRequest returningResponse:nil error:nil] encoding:NSUTF8StringEncoding];
NSLog(#"%#",returnString);
I have simply pass this category id and get the result but returnString always display all category details. I didn't get an exact result. Is this the correct way of passing xml data to webservice?
go and search for 'wsdl2objc' it does it all for you if you wanted to do soap requests.
This code will work perfectly. the problem is webservice page

how to call the sharepoint web service GetListItems using objective C

Can any one tell me how to call the SharePoint web service GetListItems. I am getting an error message "403 Forbidden" on calling this method
The code for my HTTP header request is:
[theRequest setValue: cookie forHTTPHeaderField:#"Cookie"];
[theRequest setHTTPShouldHandleCookies:YES];
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue: saction forHTTPHeaderField:#"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
urlconnection = [NSURLConnection connectionWithRequest:theRequest delegate:self];
My XML structure for calling the GetListItems methods is:
< listName >listName< /listName >< viewName >< /viewName >< Query />< ViewFields />< rowLimit>< /rowLimit>< QueryOptions>< IncludeAttachmentUrls>TRUE< /IncludeAttachmentUrls>< /QueryOptions>< webID>< /webID>< /GetListItems>< /soap:Body>< /soap:Envelope>
Can anybody tell me where I am wrong or what else I need to do?
Thanks in advance.
First of all you need ASIHTTPREQUEST, it's easy to use and very powerful. So I end up doing it like this:
NSURL *url = [NSURL URLWithString:#"http://SHAREPOINTSITE/_vti_bin/Lists.asmx"]; //Always needs to end up with _vti_bin/WHATYOUNEED.asmx
ASIHTTPRequest *asiRequest = [ASIHTTPRequest requestWithURL:url];
[asiRequest setUsername:#"USERNAME"];
[asiRequest setPassword:#"PASSWORD"];
[asiRequest setDelegate:self];
NSString *soapMessage = #"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<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\">\n"
"<soap12:Body>\n"
"<GetList xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\">\n"
"<listName>STRING</listName>\n"
"</GetList>\n"
"</soap12:Body>\n"
"</soap12:Envelope>\n";
[asiRequest appendPostData:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
[asiRequest addRequestHeader:#"Content-Type" value:#"text/xml; charset=utf-8"];
[asiRequest startAsynchronous];
Next step is to set the delegate:
- (void)requestFinished:(ASIHTTPRequest *)request
{
NSString *responseString = [request responseString];
NSLog(#"responseString : %#", responseString);
NSLog(#"Response Header: %#",[request responseHeaders] );
NSData *responseData = [request responseData];
NSLog(#"responseData : %#", responseData);
}
- (void) requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
NSLog(#"Connection Error: %#", error);
NSLog(#"Code: %i", [error code]);
NSLog(#"UserInfo: %#", [error userInfo]);
NSLog(#"Domain: %#", [error domain]);
NSLog(#"HelpAnchor: %#", [error helpAnchor]);
}

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.