gpt4 book ai didi

iOS AFNetworking GET/POST 参数

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:42:55 26 4
gpt4 key购买 nike

我想使用包含 GET 和 POST 参数的 AFNetworking 执行 POST 请求。

我正在使用这段代码:

NSString *urlString = [NSString stringWithFormat:@"upload_stuff.php?userGUID=%@&clientGUID=%@",
@"1234",
[[UIDevice currentDevice] identifierForVendor].UUIDString];

NSString *newUrl = @"https://sub.domain.com";

NSURL *baseURL = [NSURL URLWithString:newUrl];

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
[httpClient defaultValueForHeader:@"Accept"];

NSDictionary *getParams = [NSDictionary dictionaryWithObjectsAndKeys:
@"1234", @"userGUID",
[[UIDevice currentDevice] identifierForVendor].UUIDString, @"clientGUID",
nil];
NSDictionary *postParams = [NSDictionary dictionaryWithObjectsAndKeys:
[@"xyz" dataUsingEncoding:NSUTF8StringEncoding], @"FILE",
nil];

[httpClient postPath:urlString parameters:postParams success:^(AFHTTPRequestOperation *operation, id responseObject) {


}failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error retrieving data: %@", error);
}];

现在我有两个问题:

  • 如何在同一个请求中同时使用 GET 和 POST 字典?目前,我将 GET 字典集成到 URL 中并仅使用 POST 字典 ([httpClient postPath:...])

  • 我从服务器收到一条错误消息,指出缺少参数“FILE”。不幸的是我无法检查任何服务器日志(不是我的服务器)。但是使用标准的 NSURLConnection 我能够将带有 FILE 参数的请求发送到该服务器。那么这里出了什么问题?

最佳答案

为您准备的 Stackoverflow:

NSData* sendData = [self.fileName.text dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *sendDictionary = [NSDictionary dictionaryWithObject:sendData forKey:@"name"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:remoteUrl];
NSMutableURLRequest *afRequest = [httpClient multipartFormRequestWithMethod:@"POST"
path:@"/photos"
parameters:sendDictionary
constructingBodyWithBlock:^(id <AFMultipartFormData>formData)
{
[formData appendPartWithFileData:photoImageData
name:self.fileName.text
fileName:filePath
mimeType:@"image/jpeg"];
}
];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:afRequest];
[operation setUploadProgressBlock:^(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {

NSLog(@"Sent %d of %d bytes", totalBytesWritten, totalBytesExpectedToWrite);

}];

[operation setCompletionBlock:^{
NSLog(@"%@", operation.responseString); //Gives a very scary warning
}];

[operation start];

@Igor Fedorchuk 来自 POST jpeg upload with AFNetworking

关于iOS AFNetworking GET/POST 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15605026/

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