API POST方法不保存sql server中的数据

时间:2013-11-19 07:51:42

标签: ios nsurlconnection nsurlrequest

您好我是iphone开发的新手,我目前正在处理一个我有屏幕的项目,用户应在其中输入用户名和密码等详细信息。我用谷歌搜索并找出关于GET / POST / DELETE的NSURLConnection。我可以通过以下代码获取数据,

  NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
 [request setURL:[NSURL URLWithString:@"http://************/api/Users"]];
 [request setHTTPMethod:@"GET"];
 [request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"Content-
 Type"];
 NSURLResponse *response;
 NSData *GETData = [NSURLConnection sendSynchronousRequest:request   
 returningResponse:&response error:nil];
 NSString *ResultData = [[NSString alloc] initWithBytes:[GETData bytes] length:[GETData 
  length] encoding: NSASCIIStringEncoding];
 NSLog(@"ResultData: %@", ResultData);

但对于POST方法,我没有得到任何想法,它如何运作以及如何将记录存储到sql server数据库,无法理解它是否存储数据,我尝试了以下代码,

username = @"Aravind.k";
password = @"1234/";
email = @"sivaarwin@gmail.com";

NSString *post = [NSString  stringWithFormat:@"FirstName=%@&LastName=%@&WorkEmailAddress=%@",
    username, password, email];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding 
     allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://*********/api/Users"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded;charset=UTF-8"  
forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSLog(@"request: %@", request);
NSLog(@"postData: %@", postData);
NSURLResponse *response;
NSData *POSTReply = [NSURLConnection sendSynchronousRequest:request 
                                          returningResponse:&response 
                                                      error:nil];    
NSLog(@"POSTReply: %@", POSTReply);    
NSString *theReply = [[NSString alloc] initWithBytes:[POSTReply bytes] 
                                              length:[POSTReply length] 
                                            encoding:NSASCIIStringEncoding];

NSLog(@"Reply: %@", theReply);

对于GET和POST同样的api url,任何关于POST和DELETE的建议都将不胜感激,提前感谢。我们如何在输入的数据存储到服务器时收到通知。

1 个答案:

答案 0 :(得分:1)

首先关闭:不检查错误会自动导致投票减少;)

请使用完整的错误检查编辑您的代码!

使用MIME类型application/x-www-form-urlencoded时,您需要正确编码参数。

我建议,创建一个NSDictionary来保存你的参数,例如:

NSDictionary* params = @{@"FirstName": firstName, @"LastName": lastName, @"password": password};

然后使用以下链接中描述的方法获取适合用作application/x-www-form-urlencoded消息的正文数据的编码参数字符串:

How to send multiple parameterts to PHP server in HTTP post

上面的链接为NSDictionary实现了一个Category和一个方法dataFormURLEncoded,它返回NSData个对象中的编码字符串:

NSData* postData = [params dataFormURLEncoded];

注意: MIME类型application/x-www-form-urlencoded没有charset参数。它将被服务器忽略。您应该设置如下所示的标题:

[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

否则,您的代码应该有效。我强烈建议使用方便方法的异步版本:

+ (void)sendAsynchronousRequest:(NSURLRequest *)request 
                          queue:(NSOperationQueue *)queue 
              completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))handler