gpt4 book ai didi

ios - 如何在上传带有参数的图像时使用 AFNetworking 2.0 设置 cookie?

转载 作者:可可西里 更新时间:2023-11-01 03:34:30 30 4
gpt4 key购买 nike

如果我必须在发布请求中附加 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。

如果我以某种方式在内部将此 cookie 附加到 afnetworking 文件中,我仍然无法上传图像。我尝试了两种可能的方法:

第一种方式:

-(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 方法中附加了 cookie,如下所示:

- (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];
}

但是我不知道如何为这个请求添加参数。我更喜欢第二种方式。

最佳答案

能够解决它..我只用第一种方式使用它..可能两种方式都可以使用它:

这是我如何使用 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"];

在上面的代码行中,您需要准确指定参数名称和参数类型(图像)。这里必须将"file"参数传递给它(根据我从服务器端人员那里得到的 API),类型应该是“image/jpeg”(或 image/pdf 或我认为是 image/png)。您可以为文件名传递任何名称,这里我将文件名传递为:NSString *fileName = [NSString stringWithFormat:@"Prescription%d.jpg", counter];

我做的唯一错误是我没有为图像指定正确的参数,为了总结我应该像这样发送它:

  • 参数键:"file"
  • 数据:“文件或图像数据”
  • 文件名:“.jpg”//注意:扩展名图片名称必须存在
  • mime 类型:“”

关于ios - 如何在上传带有参数的图像时使用 AFNetworking 2.0 设置 cookie?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20194654/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com