美文网首页
ios – AFNetworking 3 x-www-form-

ios – AFNetworking 3 x-www-form-

作者: 可乐小子 | 来源:发表于2019-11-15 10:48 被阅读0次

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

    相关文章

      网友评论

          本文标题:ios – AFNetworking 3 x-www-form-

          本文链接:https://www.haomeiwen.com/subject/jvmjictx.html