iOS AFNetworking GET/POST 参数

标签 ios nsurlconnection afnetworking

我想使用包含 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/

相关文章:

iOS PhoneGap 应用因使用 localStorage 而被拒绝

ios - 调用popToRootViewController动画: on back button pressed

objective-c - 如果需要委托(delegate)函数,如何直接返回NSURLConnection加载的数据?

objective-c - 使用元数据分块发送文件

ios - 写入流 iOS 时的自旋锁(仅在模拟器上)

ios - 在 ios 7 中添加显示窗体底部的栏

ios - 为什么异步 NSURLConnection 会使 iOS 上的 UI 变慢?

ios - NSURLConnection 正在返回旧数据

ios - AFNetworking 的 UICollectionView 异步下载错误

iphone - AFNetworking UIImageView setImageWithURLRequest 返回响应代码 0