1

我正在尝试调用在 PHP 中创建的 web 服务。当我从 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 );

        }); 

这通过 php 代码可以正常工作,但是当我尝试从目标 C 代码发送它时,服务器无法接收参数,响应显示空值请求。我称它为:

   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
}

但是这个返回空 ID 值的响应。

4

1 回答 1

2

你可以这样做,

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

如果 currentUniqueID 为整数,@"name=%@"则为@"name=%i"

于 2013-05-30T07:45:16.430 回答