【问题标题】:AFNetworking Post request not workingAFNetworking Post 请求不起作用
【发布时间】:2023-04-03 14:01:01
【问题描述】:

我在向 .net webservice 方法发出 POST 请求时遇到问题。 我已经提出了很多帖子请求,但我似乎没有得到我在这里面临的问题 在 Rest 客户端的帮助下成功发出请求 但它总是给我一个 400 错误 我正在使用 AFNetworking 发出发布请求 如果有问题请找下面的代码

NSURL *url=[NSURL URLWithString:@"http://WebService.svc"]; 
NSMutableDictionary *params=[NSMutableDictionary dictionary];
[params setValue:@"cabbage123" forKey:@""];

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
[httpClient setParameterEncoding:AFJSONParameterEncoding];
[httpClient setDefaultHeader:@"Accept" value:@"application/json"];

[httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];

[httpClient postPath:[NSString stringWithFormat:@"Users/%@",userId] parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
    // Print the response body in text
    NSLog(@"Response: %@",responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

}];

我得到一个带有此错误的 html 响应

AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest http://WebService.svc/Users/userid>, NSErrorFailingURLKey=http://WebService.svc/Users/userid, NSLocalizedDescription=Expected status code in (200-299), got 400, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x7ba6df0>}

【问题讨论】:

  • 您是否尝试过使用“charles”跟踪此通话。确保您从 rest 客户端和 AFNetworking 发送的调用是相同的,您可能会丢失一些标头
  • 我认为我发送参数的方式有问题
  • 你也可以在这里分享解决方案,这样下次如果有人遇到类似情况,他就会为他准备好解决方案。
  • 我仍在寻找解决方案。完成后将上传

标签: ios .net web-services afnetworking


【解决方案1】:

我认为用于 WCF 网络服务的 AfNetworking 存在一些问题。 最终我可以通过使用标准的 NSURLConnection 类来连接和使用网络服务。虽然我现在面临 PUT 请求的相同问题。

【讨论】:

    【解决方案2】:

    您是否尝试过设置 AFNetworking 以允许无效证书?

    httpClient.allowsInvalidSSLCertificate = YES;
    

    如果你想使用操作。

    AFSecurityPolicy *sec=[[AFSecurityPolicy alloc] init];
    [sec setAllowInvalidCertificates:YES];
    operation.securityPolicy=sec;
    

    【讨论】:

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