【问题标题】:Issue with creating POST request ios创建 POST 请求 ios 的问题
【发布时间】:2014-01-20 18:30:31
【问题描述】:

我创建了一个包含“电子邮件”和“密码”字段的字典,并将这些参数用作我的请求正文。我从我的服务器收到我缺少参数电子邮件和密码的响应 - 我很肯定这不是服务器端的问题。这就是我形成请求的方式:

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

NSString *urlString = @"xxxxxx.xxxx.com/userServices/login";
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

NSDictionary *params = @{@"email" : email, @"password" : password};
NSData *bodyData = [NSJSONSerialization dataWithJSONObject:params options:NSJSONWritingPrettyPrinted error:nil];
[request setHTTPBody:bodyData];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setTimeoutInterval:45];
[request setHTTPShouldHandleCookies:NO];

知道为什么服务器无法识别我的参数吗?

【问题讨论】:

    标签: ios http-post nsurlrequest


    【解决方案1】:

    在创建字典时,我会这样做

    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setValue:[NSString stringWithFormat:@"%@", email] forKey:@"email"];
    [params setValue:[NSString stringWithFormat:@"%@", password] forKey:@"password"];
    

    之后我会这样做

    NSData *postData = [NSJSONSerialization dataWithJSONObject:params options:0 error:nil];
    

    您可能需要为请求指定内容长度

    [request setValue:[NSString stringWithFormat:@"%i",postData.length] forHTTPHeaderField:@"Content-Length"];
    

    【讨论】:

    • 试过了,没用。我认为我的字典正在形成,值在调试器中按预期显示。
    • 我编辑了我的答案,区别在于打包数据时的 0 个选项,而不是你拥有的选项。当我发送请求时,0 选项对我有用
    • 尝试了 0 个选项,没有区别。
    • @dMurdZ 您期望服务器端的参数是什么?
    【解决方案2】:
    NSString *post =[[NSString alloc] initWithFormat:@"email=%@&password=%@",[txtUserEmail text],[txtUserPassword text]];
               // what ever in you case e.g [txtuseremail text] replaced by email
    
                NSURL *url=[NSURL URLWithString:@"http://localhost/abc.php"];
    
                NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    
                NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
    
    
                NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    
                [request setURL:url];
                [request setHTTPMethod:@"POST"];
                [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
            [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
            [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
            [request setHTTPBody:postData];
    

    试试这个可能对你有帮助

    【讨论】:

      【解决方案3】:

      试试这个它会为你工作:

      NSDictionary * requestDict =[NSDictionary dictionaryWithObjectsAndKeys:@"email",@"YourEmail",@"password",@"YourPassword", nil];

      NSMutableURLRequest * request =[[ NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:BASE_URL]];
      
      NSData * postData =[ NSJSONSerialization dataWithJSONObject:requestDict options:0 error:nil];
      
      [request setHTTPMethod:@"POST"];
      [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
      [request setHTTPBody:postData];
      
      
      [NSURLConnection
       sendAsynchronousRequest:request
       queue:[[NSOperationQueue alloc] init]
       completionHandler:^(NSURLResponse *response,
                           NSData *data,
                           NSError *error)
       {
      
           if ([data length] >0 && error == nil)
           {
      
               NSDictionary * dictServerResponse =[ NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
      
               NSLog(@"data %@",dictServerResponse);
               // DO YOUR WORK HERE
      
           }
           else if ([data length] == 0 && error == nil)
           {
               NSLog(@"Nothing was downloaded.");
           }
           else if (error != nil){
               NSLog(@"Error = %@", error);
           }
      
       }];
      

      【讨论】:

        猜你喜欢
        • 2013-01-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多