2014-06-22 141 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並確保您使用的是正確的狀態碼。