2014-06-22 94 views
-1
-(void)uploadUserImageWithUIImage:(UIImage *)image 
{ 
    UIImage *imageTanya = [UIImage imageNamed:@"1.jpg"]; 
    AFHTTPRequestOperationManager *m = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://www.xxxxxxxx.com/yyyyyy/filesService.svc/"]]; 
    NSData *imageData = UIImageJPEGRepresentation(imageTanya, 0.5); 
    AFHTTPRequestOperation *op = [m POST:@"UploadFile" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { 
     //do not put image inside parameters dictionary as I did, but append it! 
     [formData appendPartWithFileData:imageData name:@"1" fileName:@"1.jpg" mimeType:@"image/jpeg"]; 
    } success:^(AFHTTPRequestOperation *operation, id responseObject) { 
     NSLog(@"Success: %@ ***** %@ **** %@", operation.responseString, responseObject, operation.request.URL.absoluteString); 
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     NSLog(@"Error: %@ ***** %@", operation.responseString, error); 
    }]; 
    [op start]; 
} 

同时发送请求POST它变成GET因为AFNetworking当服务器重定向它发送错误的路径的方法已经变成得到 走错了路:“http://www.xxxxxxxx.com/UploadFile HTTP/1.1” 当我创建一个新项目,它工作正常,但在我的项目,它给了我这个错误 -xcode 5.1版 - AFNetworking 2.0AFNetworking发送GET而不是POST

回答

0

试试这个:

m POST:[NSString stringWithFormat:@"http://www.mohamedaspnet.com/Nazzelha/filesService.svc/%@", @"UploadFile"] .... 

对不起,我没有得到什么问题,如果你正在寻找POST到一个URL,但服务器将它重定向到另一个URL,例如,http://domain.com/abc ==> 301 ==>http://domain.com/def,那么什么你期望被张贴到http://domain.com/def实际上,你应该看看这个线程AFNetworking: setRedirectResponseBlock inside my class NSURLResponse always return null value

根据对相应的委托方法, 连接的文档:willSendRequest:redirectResponse ::

的委托可以收到此消息,因为它在发送之前修改了 请求,例如用于转换请求的URL 为其规范形式。要检测这种情况,请检查redirectResponse; 如果它为零,则由于重定向而未发送消息。

+0

它也发送GET –

0
  1. 你的请求是由基金会网址加载系统处理,而不是AFNetworking问题。

  2. 您应该删除这条线,因为POST:…方法你已经调用入列操作:

    [op start]; 
    
  3. 检查您的重定向服务器发送的类型。如果它发送HTTP 303,则RFC 2616要求将请求类型更改为GET。它应该发送HTTP 307.请参阅3xx Redirection并确保您使用的是正确的状态码。