ios – AFNetworking 3 x-www-form-urlencoded发布数据

前端之家收集整理的这篇文章主要介绍了ios – AFNetworking 3 x-www-form-urlencoded发布数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用x-www-form-urlencoded正文发布数据.
通过邮递员发帖,没关系

但是我不能通过afnetworking来做到这一点.这是我的代码

NSDictionary *parameters = @{@"login"   : email,@"password": password};

NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameters
                                                   options:0
                                                     error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

self.requestSerializer = [AFJSONRequestSerializer serializer];

NSString *urlString = [NSString stringWithFormat:@"%@/%@",HTTPBaseRequestURL,appendLoginUrl];

NSLog(@"URL %@\njsonString %@",urlString,jsonString);


[self POST:urlString parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
    [formData appendPartWithFormData:jsonData name:@"data"];
} progress:nil success:^(NSURLSessionDataTask * _Nonnull task,id  _Nullable responSEObject) {        
    onSuccess(responSEObject);
} failure:^(NSURLSessionDataTask * _Nullable task,NSError * _Nonnull error) {
    NSString *errorDescription = [NSError serverErrorMessageFromData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey]];
    NSInteger statusCode = [NSHTTPURLResponse errorCode:(NSHTTPURLResponse*)task.response];

    NetworkRequestError *requestError = [[NetworkRequestError alloc] initWithType:
                                         (NSHTTPURLResponse*)task.response ? NetworkRequestErrorTypeServerError : NetworkRequestErrorTypeNoConnection
                                                                      description:
                                         (NSHTTPURLResponse*)task.response ? errorDescription : nil];
    requestError.statusCode = statusCode;

    NSLog(@"Error from server: %@,status code = %ld,error type = %lu",requestError.errorDescription,(long)requestError.statusCode,(unsigned long)requestError.type);
    onFailure(requestError);
}];

请帮助我理解如何正确地做到这一点.谢谢!

解决方法

评论后我终于找到了答案.这是我现在正常运行的请求,请注意添加
[manager.requestSerializer setValue:@"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];

这是完整的代码

NSString *url = [NSString stringWithFormat:@"%@%@",APIBASE,APIUSERENDPOINT];

NSDictionary* parametersDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                          username,@"username",password,@"password",nil
                          ];

AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    [manager.requestSerializer setValue:@"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];

[manager POST:url parameters:parametersDictionary progress:nil success:^(NSURLSessionDataTask * _Nonnull task,id  _Nullable responSEObject) {
    NSLog(@"%@",responSEObject);
} failure:^(NSURLSessionDataTask * _Nullable task,NSError * _Nonnull error) {
    NSLog(@"%@",error);
}];
原文链接:https://www.f2er.com/iOS/328251.html

猜你在找的iOS相关文章