3

我想使用包含 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参数的请求发送到该服务器。那么这里出了什么问题呢?

4

2 回答 2

0

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 使用 AFNetworking 上传

于 2013-03-27T02:27:51.770 回答
0

AFNetworking 无法同时设置 GET 和 POST 参数。您必须为您的 url 设置 GET 参数,并使用[AFHTTPClient requestWithMethod:path:parameters:]设置 POST 参数。

- (NSURLRequest *)requestForPath:(NSString *)path method:(NSString *)method
{
    NSMutableString *pathWithGetParams = [NSMutableString stringWithString:path];
    BOOL hasPathContainsQueryChar = [path rangeOfString:@"?"].location != NSNotFound;
    [pathWithGetParams appendString:hasPathContainsQueryChar ? @"&" : @"?"];
    for (id key in self.getArguments.allKeys)
    {
        if ([key isKindOfClass:[NSString class]])
        {
            NSString *value = self.getArguments[key];
            if ([value isKindOfClass:[NSString class]])
            {
                [pathWithGetParams appendString:[[self class] urlEncode:key]];
                [pathWithGetParams appendString:@"="];
                [pathWithGetParams appendString:[[self class] urlEncode:value]];
                [pathWithGetParams appendString:@"&"];
            }
        }
    }

    NSString *upperCaseMethod = [method uppercaseString];
    BOOL isMethodInGet = [upperCaseMethod isEqualToString:@"GET"];
    NSURLRequest *request = [[self shareAFClient] requestWithMethod:method
                                                               path:pathWithGetParams
                                                         parameters:isMethodInGet ? nil : self.postArguments];
    return request;
}

+ (NSString *)urlEncode:(NSString *)stringToEncode
{
    return [self urlEncode:stringToEncode usingEncoding:NSUTF8StringEncoding];
}

+ (NSString *)urlEncode:(NSString *)stringToEncode usingEncoding:(NSStringEncoding)encoding
{
    return (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
                                                                                 (__bridge CFStringRef)stringToEncode,
                                                                                 NULL,
                                                                                 (CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ",
                                                                                 CFStringConvertNSStringEncodingToEncoding(encoding));
}

+ (NSString*)urlDecode:(NSString *)stringToDecode
{
    return [self urlDecode:stringToDecode usingEncoding:NSUTF8StringEncoding];
}

+ (NSString*)urlDecode:(NSString *)stringToDecode usingEncoding:(NSStringEncoding)encoding
{
    return (__bridge_transfer NSString *) CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL,
                                                                                                  (__bridge CFStringRef)stringToDecode,
                                                                                                  (CFStringRef)@"",
                                                                                                  CFStringConvertNSStringEncodingToEncoding(encoding));
}
于 2013-11-05T06:03:03.150 回答