【问题标题】:Objective C - HTTP POST REQUEST handle with header and paramsObjective C - 带有标头和参数的 HTTP POST REQUEST 句柄
【发布时间】:2017-04-10 11:31:31
【问题描述】:

我已经使用 POST 方法在我的应用程序中调用带有标头值和参数的 API。 但在我的后端无法让我的标头值进行身份验证。甚至我的回复也有无效用户。我附上了我的sn-p。你能帮我解决这个问题吗?

NSString *post = [NSString stringWithFormat:@"test=Message"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

[request setURL:[NSURL URLWithString:@"http://myURL.com/FakeURL"]];
[request setHTTPMethod:@"POST"];

NSString *authStr=[NSString stringWithFormat:@"userName:xxx"];
NSData *authData=[authStr dataUsingEncoding:NSUTF8StringEncoding];
NSString *headerValue=[NSString stringWithFormat:@"Basic %@",[authData base64EncodedStringWithOptions:0]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setValue:headerValue forHTTPHeaderField:@"Authorization"];


NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"requestReply: %@", requestReply);

我的回复是:

2017-04-10 16:43:35.635 Testing[5155:172935] ST :200
2017-04-10 16:43:35.636 API_Testing_AeroVoyce[5155:172935] Error :(null)
2017-04-10 16:43:35.638 API_Testing_AeroVoyce[5155:172935] requestReply :invalid user

【问题讨论】:

  • 你是如何传递 URL 的?
  • @AbilashBNair 抱歉,现在我更新了 URL myURL.com/FakeURL
  • 它可以与 POSTMAN 一起使用吗?
  • 看看如何在目标 C 中添加标头。我认为这是您在构建请求时遇到问题的地方。尝试断点以了解您发送的真实请求。 stackoverflow.com/a/5116201/6203030
  • headerValue 是否与您在 POSTMAN 上的相同?既然你有application/x-www-form-urlencoded,你有没有添加那个标题?

标签: ios object authentication http-post nsurlsession


【解决方案1】:

为授权、URL 和内容长度创建单个 URL 请求:

  1. 您在请求标头字段中缺少内容长度。

     - (void)postRequest
    
     {
       NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
        NSString *post = [NSString stringWithFormat:@"test=Message"];
        NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
        NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
    
        [request setURL:[NSURL URLWithString:@"http://myURL.com/FakeURL"]];
        [request setHTTPMethod:@"POST"];
    
        NSString *authStr=[NSString stringWithFormat:@"userName:xxx"];
        NSData *authData=[authStr dataUsingEncoding:NSUTF8StringEncoding];
        NSString *headerValue=[NSString stringWithFormat:@"Basic %@",[authData base64EncodedStringWithOptions:0]];
    
        [request setValue:headerValue forHTTPHeaderField:@"Authorization"];
    
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    
    
        NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
        NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
            NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
            NSLog(@"requestReply: %@", requestReply);
        }];
        [task resume];
    }
    
  2. 确保您已添加 NSURLSessionDataTask 以恢复。

  3. 还要检查您是否已在 info.Plist 中启用 App Transport Security

这将有助于解决您的问题。

编辑:

尝试这样做:

 if(data!= nil)
        {
            id JSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            NSLog(@"Response === %@",JSON);
        }

代替:

NSString *requestReply = [[NSString alloc] initWithData:数据编码:NSASCIIStringEncoding];

【讨论】:

  • 是的。我在 info.Plist 中启用了 App Transport Security
  • 我会试试 NSURLSessionDataTask
  • @Ablilase B nair 即使我用 NSURLSessionDataTask 打电话但我得到了以下问题。我已经检查了后端。他们告诉您没有在您的标题字段中获得您的凭据。错误域 = NSCocoaErrorDomain 代码 = 3840 “无值。” UserInfo={NSDebugDescription=无值。
  • 是的,我都试过了 NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; AND NSDictionary *Json_value = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];当我使用 NSString 时我得到空响应,当我被击中 NSDictionary 时我得到空响应
  • 试试“NSJSONReadingAllowFragments”而不是 NSJSONReadingMutableLeaves
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-09
  • 2014-05-14
  • 2013-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多