2013-11-25 28 views
7

如果我不得不附加一個cookie和post requst,該怎麼辦?我應該怎麼做?如何在使用參數上傳圖像時使用AFNetworking 2.0設置Cookie?

NSURL *URL = [NSURL URLWithString:addAddressUrl]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL]; 

// Set cookie too 
NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:@"<URL>"]]; 
NSDictionary *cookiesDictionary = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies]; 

if(cookiesDictionary) { 
    [request setAllHTTPHeaderFields:cookiesDictionary]; 
} 

如何將此請求附加到AFNetworking調用? 我已經瀏覽了AFNetworking的文檔,但並未解釋如何在具有其管理器對象的請求中設置cookie。

而iF我一些如何將這個cookie添加到內部網絡文件中,但我仍然無法上傳圖片。我已經試過它的兩種可能的方式:

第一種方式:

-(void)uploadPrescriptionImage :(UIImage *)imagePresc 
{ 
    // upload image here on the prescription 
    /* 
    Uploading a prescription 
    URL: <URL> 
    Params: <PARAMS> 
    Method: POST 
    */ 


    NSData *imageData = UIImageJPEGRepresentation(imagePresc, 1.0); 

    NSString *orderID = [[NSUserDefaults standardUserDefaults] valueForKey:@"orderId"]; // orderId 

    NSDictionary *parameters = @{@"docName":prescriptionCell.doctorNameTextField.text,@"patientName":prescriptionCell.patientNameTextField.text,@"orderId" : orderID}; 

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
    [manager POST:@"<URL>" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) 
    { 
     [formData appendPartWithFileData:imageData name:@"prescription" fileName:@"prescription" mimeType:@"image/jpeg"]; 
    } 
      success:^(AFHTTPRequestOperation *operation, id responseObject) 
    { 
     NSLog(@"response is : %@",responseObject); 
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) 
    { 
     NSLog(@"Error: %@ *****", [error description]); 
    }]; 
} 

我在afnetworking方法atttached餅乾象下面這樣:

- (AFHTTPRequestOperation *)POST:(NSString *)URLString 
         parameters:(NSDictionary *)parameters 
     constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block 
         success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success 
         failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure 
{ 
    NSMutableURLRequest *request = [self.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters constructingBodyWithBlock:block]; 
    NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:@"<URL>"]]; 
    NSDictionary *cookiesDictionary = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies]; 
    if (cookiesDictionary) { 
     [request setAllHTTPHeaderFields:cookiesDictionary]; 
    } 
    AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure]; 
    [self.operationQueue addOperation:operation]; 

    return operation; 
} 

方式二:

NSDictionary *parameters = @{@"docName":@"rr",@"patientName":@"tt",@"orderId" : @"1"}; 


    NSString *URLString = @"<URL>"; 
// 
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; 
// 
    NSURL *URL = [NSURL URLWithString:URLString]; 
    NSMutableURLRequest *request = [NSURLRequest requestWithURL:URL]; 

    // Set cookie too 
    NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:@"<URL>"]]; 
    NSDictionary *cookiesDictionary = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies]; 
    if (cookiesDictionary) { 
     [request setAllHTTPHeaderFields:cookiesDictionary]; 
    } 
// 
// NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"]; 
    NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromFile:[NSURL URLWithString:filePathOfImage] progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) 
    { 
     if (error) { 
      NSLog(@"Error: %@", error); 
     } else { 
      NSLog(@"Success: %@ %@", response, responseObject); 
     } 
    }]; 
    [uploadTask resume]; 
} 

但我不知道如何添加這個請求的參數..常見馬特湯普森..請幫我這是maki NG frushtrated大家..我想喜歡第二種方式..

回答

1

能夠解決it..I也只有第一種方式使用它..可能是能夠同時在使用方式:

這裏是我的我是如何能夠與afnetworking 2.0上傳圖片代碼:

NSString *fileName = [NSString stringWithFormat:@"Prescription%d.jpg", counter]; 

    NSData *imageData = UIImageJPEGRepresentation(imagePresc, 1.0); 

    NSString *orderID = [[NSUserDefaults standardUserDefaults] valueForKey:@"orderId"]; // orderId 

    NSDictionary *parameters = @{@"docName":prescriptionCell.doctorNameTextField.text,@"patientName":prescriptionCell.patientNameTextField.text,@"orderId" : orderID}; 

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
    [manager POST:@"http://staging.healthkartplus.com/webservices/prescription/upload" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData){ 
     [formData appendPartWithFileData:imageData name:@"file" fileName:fileName mimeType:@"image/jpeg"]; 

    }success:^(AFHTTPRequestOperation *operation, id responseObject) 
    { 
     NSLog(@"response is : %@",responseObject); 
    }failure:^(AFHTTPRequestOperation *operation, NSError *error) 
    { 
     NSLog(@"Error: %@ *****", [error description]); 
    }]; 

這段代碼的重要組成部分是:

[formData appendPartWithFileData:imageData name:@"file" fileName:fileName mimeType:@"image/jpeg"]; 

在這裏,在上面的代碼行,你需要準確指定參數的名稱和參數的類型(圖像)。這裏的「文件」參數必須傳遞給它(根據我從服務器端傢伙得到的API),類型應該是「image/jpeg」(或者image/pdf或者我認爲是image/png)。您可以爲文件名傳遞任何名稱,我在這裏傳遞文件的名稱爲:NSString *fileName = [NSString stringWithFormat:@"Prescription%d.jpg", counter];

我在做唯一的錯誤是我沒有指定正確的參數爲圖像,以summerise我應該送什麼樣子:

  • 參數鍵: 「文件」
  • 數據: 「文件或圖像數據」
  • 文件名: 「.JPG」 //注意: 圖像名稱的擴展名必須是有
  • MIME類型: 「」
相關問題