使用AFNetWorking 上传字符串,提示:Request failed: 请求太大


我要把图片换成nsdata,然后再用base64encoding,把encoding后生成的字符串上传到
服务器,就出这个问题,
报错信息:err:Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: 请求太大 (413)"
我同事用的ASIHTTPRequest库就没问题,请问如何解决?
代码:


 UIImage *img = [self createThumbnailWithSourceImge:sourceImg andNewSize:sz];

       data  = UIImageJPEGRepresentation(img, 0.01);
        NSString *encodedSTR  = [data base64Encoding];
                NSString *currentUrl = @"/app/b/test";
        NSString *urlStr = [NSString stringWithFormat:@"%@%@?picStr=%@",BASEURL,currentUrl,encodedSTR];
        NSURL *url = [NSURL URLWithString:urlStr];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
                 AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc]initWithRequest:request];
    op.responseSerializer = [AFJSONResponseSerializer serializer];
    [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSLog(@"success:%@",responseObject);
        successBlock(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failureBlock(error);
    }];
    [op start];


     //   [manager POST:urlStr parameters:nil
     //         success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //          NSLog(@"success:%@",responseObject);
         //     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         //         NSLog(@"err:%@",error);
         //     }];

ios

3rd-eye 9 years, 3 months ago

问题的原因是你走的是GET请求,因为GET请求参数长度是有限制的,例如apache默认是2MB(不知道记错没有),所以如果你图片超过了这个大小,就失败了,你可以把请求改成POST试试。

茧墨あざか answered 9 years, 3 months ago

图片转换成 NSData 之后就不用再转 String 了,直接上传就行了。


 AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString: BASEURL]];
NSData *imageData = UIImageJPEGRepresentation(img, 0.1);
NSDictionary *parameters = @{@"你们需要的参数": XXXXXXX};
AFHTTPRequestOperation *op = [manager POST:currentUrl parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

    [formData appendPartWithFileData:imageData name:paramNameForImage fileName:@"photo.jpg" mimeType:@"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@ ***** %@", operation.responseString, error);
}];
[op start];

黑色餐巾纸 answered 9 years, 3 months ago

我的报404的错误 "Request failed: unacceptable content-type: text/html" 我也想知道怎么解决?
Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: not found (404)" UserInfo=0x190856b0 {com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x17d22ca0>

Sean. answered 9 years, 3 months ago

你好 我想问下iOS点击消息通知时,怎么跳转到相应的控制器中,求帮忙 感谢了 我的Q 是1144513732 拜托 拜托了 无计可施了

萝卜人酋长 answered 9 years, 3 months ago

Your Answer