1

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.

2
  • That's not how you send post parameters. stackoverflow.com/questions/5537297/… Commented May 30, 2013 at 7:39
  • you want to create request like this http://166.178.11.18/server/xs/reliable.php?name= currentUniqueID right? Commented May 30, 2013 at 7:41

1 Answer 1

2

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"

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.