API POST 方法不在 sql server 中保存数据 [英] API POST method not saving data in sql server

查看:56
本文介绍了API POST 方法不在 sql server 中保存数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hi I'm new to iphone development, I'm currently working with a project where I have a screen, in which user should enter their details, like username and password. I googled and find out about NSURLConnection for GET/POST/DELETE. I can GET data by the below codes,

  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 数据库,无法理解它是否存储数据,我尝试了以下代码,

But for POST method, i doesn't get any ideas of , how it functions and how it store record to sql server database, can't understand whether it s storing data or not, i tried the following codes,

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 的任何建议将不胜感激,提前致谢.以及我们如何在输入的数据存储到服务器中时得到通知.

Same api url for both GET and POST, any suggestions regarding POST and DELETE will be grateful, Thanks in advance.And how we can get notified as the entered data is stored into the server.

推荐答案

首先:不检查错误会自动导致否决票;)

First off: not checking for errors automatically leads to down votes ;)

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

Please edit your code with full error checks!

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

When using a MIME type application/x-www-form-urlencoded you need to properly encode the parameters.

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

I would suggest, to create a NSDictionary holding your parameters, e.g.:

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

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

and then use the approach described in the following link to get an encoded parameter string suitable for using as a body data for a application/x-www-form-urlencoded message:

如何发送HTTP post 中 PHP 服务器的多个参数

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

The link above implements a Category and a method dataFormURLEncoded for a NSDictionary which returns an encoded string in a NSData object:

NSData* postData = [params dataFormURLEncoded];

注意: MIME 类型 application/x-www-form-urlencoded 没有字符集参数.它将被服务器忽略.您应该像下面这样设置标题:

Note: The MIME type application/x-www-form-urlencoded does not have a charset parameter. It will be ignored by the server. You should set the header like below:

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

否则,您的代码应该可以工作.我强烈建议使用方便方法的异步版本:

Otherwise, your code should work. I would strongly recommend to use the asynchronous version of the convenient method:

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

这篇关于API POST 方法不在 sql server 中保存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆