【问题标题】:API POST method not saving data in sql serverAPI POST方法不在sql server中保存数据
【发布时间】:2013-11-19 07:51:41
【问题描述】:

您好,我是 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 的建议将不胜感激,在此先感谢。以及当输入的数据存储到服务器时我们如何得到通知。

【问题讨论】:

    标签: ios nsurlconnection nsurlrequest


    【解决方案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 没有字符集参数。它将被服务器忽略。您应该像下面这样设置标题:

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

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

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

    【讨论】:

    • 感谢您的回复,正如您的回复,我尝试了 NSURLSession,但没有得到结果。控制台中的结果是,{ status code: 400, headers { "Cache-Control" = "no-cache"; “内容长度”= 0;日期 =“格林威治标准时间 2013 年 11 月 20 日星期三 05:05:39”;过期=“-1”; Pragma = "无缓存";服务器 = "微软-IIS/7.5"; “X-AspNet-版本”=“4.0.30319”; "X-Powered-By" = "ASP.NET"; } } (null) 2013-11-20 10:35:39.723 SessionApp[27223:a0b] 数据 =
    猜你喜欢
    • 2018-02-22
    • 2011-01-01
    • 1970-01-01
    • 2021-05-14
    • 2011-03-30
    • 2014-11-22
    • 1970-01-01
    • 2015-07-21
    • 1970-01-01
    相关资源
    最近更新 更多