Iphone to WCF Service - iphone

I've been trying to get my iPhone application to talk to a WCF service. My application successfully connects/finds the web service, but, never seems to get a non-error response back. When I use visual studios wcf tester it works fine.
Xml format: <mAccess><user></user><pwd></pwd></mAccess>
Is there something wrong with how I'm structuring my head/body?
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:website]];
[request setHTTPMethod:#"POST"];
[request setValue:#"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-type"];
NSString *soapMessage = [[NSString alloc] initWithFormat:#"<SOAP-ENV:Envelope
xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Body>
<MobileAccess xmlns=\"http://tempuri.org/\"><mAccess><user>user</user><pwd>pwd</pwd><custID>0</custID></mAccess>
</MobileAccess></SOAP-ENV:Body></SOAP-ENV:Envelope>"];
[request setValue:[NSString stringWithFormat:#"%d",[soapMessage length]] forHTTPHeaderField:#"Content-length"];
[request addValue:#"http://tempuri.org/IProviderDataService/MobileAccess" forHTTPHeaderField:#"Soapaction"];
[request setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

There is one potential error in your construction of the URL request: The "Content-Length" must be set to the length of the HTTP body. This might be different from [soapMessage length] if non-ASCII characters in the message are converted to UTF-8.
So you should use the length of the data after the conversion:
NSData *bodyData = [soapMessage dataUsingEncoding:NSUTF8StringEncoding];
[request setValue:[NSString stringWithFormat:#"%d",[bodyData length]] forHTTPHeaderField:#"Content-length"];
[request setHTTPBody:bodyData];

Related

sending a JSON to a Server with REST API

I am developing an application with REST full web service .Now i have some problem in this case ,ie string sent to a servier using put method. I am using the following code.
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:#"http://176.31.113.164/app/profil/add"]];
[request setValue:#"Digest adf#email.com:test" forHTTPHeaderField:#"Authorization"];
[request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[request setValue:#"application/json" forHTTPHeaderField:#"content-type"];
//[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
NSDictionary *dic=[[NSDictionary alloc]init ];
dic=[NSDictionary dictionaryWithObjectsAndKeys:#"15",#"#idUsers",#"17",#"#idProfils",#"ddduu",#"address",#"ddd",#"city",#"ddd",#"country",#"adf#email.com <mailto:adf#email.com>",#"email",#"1",#"enabled",#"fff55",#"firstName",#"fffdd3",#"lastName",#"fffffcc",#"nickName",#"test",#"password",#"0",#"phone",#"0",#"postCode",#"ROLE_USER",#"role", nil];
NSString *jsonString = [jsonWriter stringWithObject:dic];
NSData *postData= [jsonString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
[request setHTTPBody:postData];
[request setHTTPMethod:#"PUT"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
NSURLConnection* _loginConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
if (_loginConnection==NULL)
{
NSLog(#"Problem");
}
[_loginConnection start];
The received data is handled as this:
NSString* tempString =[NSJSONSerialization
JSONObjectWithData:_receivedData
options:kNilOptions
error:&error];
You can use ASIHTTP wrapper class to perform REST operations in your iOS application.
Download the latest ASIHTTP from here.

Weird Error in NSURLConnection on iPad

I am making an iPad application and using NSURlConnection to connect to one of my web services to retrieve some data from the server. I am calling the webservice in such a way that it retrieves 20 records each time.
Sometimes after 10-12 calls the NSURLConnection the DidFailWithError delegate will be called. The error log says:
The Internet connection appears to be offline
I have a retry button there as well. I keep on retrying but it keeps on getting the same error and goes to DidFailWithError.
Can anybody tell me what the problem is?
Here is the code I am using:
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [soap length]];
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue: #"get_all_user_groups" forHTTPHeaderField:#"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soap dataUsingEncoding:NSUTF8StringEncoding]];
NSUrlConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

NSMutableurlrequest http headerparameter containing newline gets lost

as the title says, can't get it to work...
Nothing strange about the code:
NSMutableURLRequest *request = [self prepareRequest:inUrl];
[request addValue:[NSString stringWithFormat:#"%#", note] forHTTPHeaderField:NOTE];
....
[[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
So, this all works fine, and it arrives at my tomcat java spring service fine. EXCEPT for if the "note" string contains newline characters! looks fine when i debuglog in the client, but on my server the parameter is NULL.
Not sure if i need to urlencode or escape something?
Anybody have any pointers? Thankful for input.
Make sure you use correct mode when you send data to the server.
[request setHTTPMethod:#"POST"];
The default HTTP method is “GET”
Update:
Here is how I would generally send data to the server. The parameters may vary depending on your content type.
NSData* data = [bodyString NSUTF8StringEncoding];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:data];
[request setValue:[NSString stringWithFormat:#"%d", [data length]] forHTTPHeaderField:#"Content-Length"];
[request setValue:#"text/xml" forHTTPHeaderField:#"Content-Type"];

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

SOAP Xml issue in iPhone

Methods which take no parameter give correct result as expected. Whereas methods of webservice requiring few parameter returns no relevant result instead show Message.
Im using following code:
NSString *post =[[NSString alloc] initWithFormat:#"param1=%#&param2=%#&param3=%#&param4=%#&pageSize=%#&pageNo=%#",#"",#"81",#"1",#"",#"40",#"1"];
NSURL *url = [NSURL URLWithString:#"www.someurlxyz.com"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
soapMsg = [soapMsg stringByAppendingString:post];
NSLog(#"value of soap is %#",soapMsg);
NSString *msgLength = [NSString stringWithFormat:#"%d", [soapMsg length]];
//postData =[postData stringByAppendingString:];
[req addValue:#"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[req addValue:#"http://www.someurlxyz.com/Search/Methodname?" forHTTPHeaderField:#"SOAPAction"];
[req addValue:msgLength forHTTPHeaderField:#"Content-Length"];
[req setHTTPMethod:#"POST"];
[req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
[post release];
Message (which is getting returned is following).
Server did not recognize the value of HTTP Header SOAPAction: http://www.someurlxyz.com/Search/Methodname?
Might be a problem with the namespaces or you might be calling a wrong method.
Use some third party client like SoapUI to check your webservice, and check whether you creating exact string in your request in iPhone app.
What is the server expecting here?
It looks like you are sending param1=&param2=81&param3=1&param4=&pageSize=40&pageNo=1
Which doesn't look like an actual soap message or even XML.
You don't specify what soapMsg is originally but you're appending the string above to it.
Maybe you intended to format the original soapMgs string?