AF网络错误

时间:2015-08-13 07:00:46

标签: ios objective-c afnetworking

NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:serverUrl parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

    [formData appendPartWithFileData:data name:@"post_media" fileName:imageName mimeType:@"image/jpeg"];

    [formData appendPartWithFormData:[[requestDict objectForKey:@"user_statusmessage"] dataUsingEncoding:NSUTF8StringEncoding] name:@"user_statusmessage"];

    [formData appendPartWithFormData:[[requestDict objectForKey:@"user_description"] dataUsingEncoding:NSUTF8StringEncoding] name:@"user_description"];

    [formData appendPartWithFormData:[[requestDict objectForKey:@"id"] dataUsingEncoding:NSUTF8StringEncoding]   name:@"id"];


    [request addValue:self.header forHTTPHeaderField: @"X-API-KEY"];

    [request addValue:@"application/json" forHTTPHeaderField: @"Content-Type"];

} error:nil];

AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSProgress *progress = nil;

NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    if (error) {
        NSLog(@"Error: %@", error);
    } else {
        NSLog(@"%@ %@", response, responseObject);
    }
}];

[uploadTask resume];

,错误是

错误Domain = com.alamofire.error.serialization.response Code = -1011“请求失败:禁止(403)”UserInfo = 0x181643d0 {com.alamofire.serialization.response.error.response = {URL:{{3 } {}代码:403,headers {     “Cache-Control”=“no-store,no-cache,must-revalidate,post-check = 0,pre-check = 0”;     连接=“保持活力”;     “Content-Encoding”= gzip;     “内容长度”= 63;     “Content-Type”=“application / json; charset = utf-8”;     Date =“星期四,2015年8月13日06:48:49 GMT”;     Expires =“Thu,1981年11月19日08:52:00 GMT”;     “Keep-Alive”=“timeout = 5,max = 100”;     Pragma =“no-cache”;     Server =“Apache / 2.2.31(Unix)mod_ssl / 2.2.31 OpenSSL / 1.0.1e-fips mod_bwlimited / 1.4”;     Vary =“Accept-Encoding,User-Agent”;     “X-Powered-By”=“PHP / 5.5.27”; ,NSErrorFailingURLKey = http://myrequesturl.net/folder,NSLocalizedDescription =请求失败:禁止(403),

2 个答案:

答案 0 :(得分:0)

AFHTTPClient 示例中,您将默认编码设置为AFJSONParameterEncoding,默认情况下AFHTTPRequestOperationManager使用 AFHTTPRequestSerializer ,因此您需要设置 requestSerializer 。< / p>

manager.requestSerializer = [AFJSONRequestSerializer serializer];

答案 1 :(得分:0)

错误说明了一切:你从服务器得到400响应,这意味着你发送的内容要么格式不正确,要么服务器无法理解。 请参阅以下可能对您有所帮助的链接:

https://github.com/AFNetworking/AFNetworking/issues/1729