Reputation: 77
I am trying to call webservice created in PHP.The request and response is succesfull when i send this from php :
$.post(
'http://166.178.11.18/server/xs/reliable.php',
{"name":value2}, // any data you want to send to the script
function( data ){
$( 'body ').append( data );
});
This works perfectly fine through php code but when i am trying to send it from objective C code,server is not able to receive the parameters ,response shows request for null value. I am calling it as:
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest setHTTPMethod:@"POST"];
[theRequest setValue:currentUniqueID forKey:@"name"];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
// Do something here
}
But this one returns response for null ID value.
Upvotes: 1
Views: 1755
Reputation: 9143
You can do it like this,
NSString *post =[[NSString alloc] initWithFormat:@"name=%@",currentUniqueID];
NSLog(@"post ==> %@", post);
NSURL *url=[NSURL URLWithString:@"http://166.178.11.18/server/xs/reliable.php"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
//NSLog(@" route id is :%@",postLength);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *response = nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if ([response statusCode] >=200 && [response statusCode] <300)
{
NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"Response ==> %@", responseData);
} else {
if (error) NSLog(@"Error: %@", error);
}
If currentUniqueID is integer than @"name=%@"
will be @"name=%i"
Upvotes: 2