7

我正在尝试使用 x-www-form-urlencoded 正文发布数据。通过邮递员发帖,没问题 通过邮递员发帖,没问题

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

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);
}];

请帮助我了解如何正确执行此操作。谢谢!

4

3 回答 3

9

发表评论后,我终于找到了答案。这是我现在正常运行的请求,请注意添加

[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);
}];
于 2016-04-19T17:17:40.373 回答
1

尝试附加自定义标题信息,例如:

[self.requestSerializer setValue:@" application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-Type];

希望它对你有帮助。

于 2016-04-08T08:34:29.140 回答
0

这里 。它对我有用。太简单 。

 NSDictionary* parametersDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                      @"deviceTokenIOS", @"db487c983ebbe7c2fb066d292bb4318175f54ab27b6b9df7871907e1d0ed62ba",
                                      @"message", @"Hello Dunglt",
                                      nil
                                      ];

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"db487c983ebbe7c2fb066d292bb4318175f54ab27b6b9df7871907e1d0ed62ba", @"deviceTokenIOS", @"Hello Dunglt", @"message", nil];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];

[manager POST:[NSURL URLWithString:url].absoluteString parameters:dict progress:nil success:^(NSURLSessionTask *task, id responseObject) {
    NSLog(@"%@", responseObject);
}
      failure:^(NSURLSessionTask *operation, NSError *error) {
          NSLog(@"Error: %@", error);
      }];}
于 2016-10-30T06:27:13.217 回答