0

我有以下方法用于创建一个NSMutableURLRequest包含在NSMutableDictionary.

+ (NSMutableURLRequest *)createURLRequestWithURL:(NSString *)URL andPostData:(NSMutableDictionary *)postDictionary {
    NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc]init];
    NSString *postString = @"";
    NSString *postLength = nil;
    NSData *postData = nil;

    //convert post distionary into a string
    if (postDictionary) {
        for (NSString *key in postDictionary) {
            if ([postString length] != 0) {
                postString = [postString stringByAppendingString:@"&"];
            }

            postString = [postString stringByAppendingFormat:@"%@=%@", [self urlEncodeString:key], [self urlEncodeString:[postDictionary valueForKey:key]]];
        }
    }

    //get length of post and convert post into a data object
    postLength = [NSString stringWithFormat:@"%d", [postString length]];
    postData = [postString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    //setup the request
    [urlRequest setURL:[NSURL URLWithString:URL]];
    [urlRequest setHTTPMethod:@"POST"];
    [urlRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [urlRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [urlRequest setHTTPBody:postData];

    return urlRequest;
}

到目前为止,我一直只使用字符串数据作为postDictionary.

我的问题是我应该如何修改此代码以发送其他类型的值?具体来说,我正在尝试将图像发送到服务器。

4

3 回答 3

1

http://lists.apple.com/archives/web-dev/2007/Dec/msg00017.html

NSString *filename = @"myfile.jpg";
NSString *boundary = @"----FOO";

 NSURL *url = [NSURL URLWithString:@"http://example.com";];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:@"POST"];

NSString *contentType = [NSString stringWithFormat:@"multipart/form- data, boundary=%@", boundary];
[req setValue:contentType forHTTPHeaderField:@"Content-type"];

NSString *imagePath = [NSString stringWithFormat:@"/path/to/image/%@", filename];
NSData *imageData = [NSData dataWithContentsOfFile:imagePath options:0 error:nil];

//adding the body:
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Disposition: form-data; name= \"some_name\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"some_value"; dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content- Disposition: form-data; name=\"image_file\"; filename=\"%@\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:imageData];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Disposition: form-data; name= \"some_other_name\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"some_other_value"; dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r \n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[req setHTTPBody:postBody];

或者只使用 ASIHttpRequest http://allseeing-i.com/ASIHTTPRequest/

于 2013-01-08T15:04:49.480 回答
1

您应该查看本教程

基本上你需要

  1. 更改内容类型标题
  2. 从图像中获取 NSDate(例如使用 UIImageJPEGRepresentation())
  3. 使用图像数据、边界、Content-Disposition 和 Content-Type 创建主体
于 2013-01-08T15:07:14.857 回答
0

使用其他答案中的信息根据需要修改我的代码:

+ (NSMutableURLRequest *)createURLRequestWithURL:(NSString *)URL andPostData:(NSMutableDictionary *)postDictionary {
    NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc]init];
    NSMutableData *postData = [NSMutableData data];

    NSString *boundary = @"---------------------------14737809831466499882746641449";

    //convert post distionary into a string
    if (postDictionary) {
        for (NSString *key in postDictionary) {
            [postData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

            id postValue = [postDictionary valueForKey:key];

            if ([postValue isKindOfClass:[NSString class]]) {
                [postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name= \"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
                [postData appendData:[postValue dataUsingEncoding:NSUTF8StringEncoding]];
            } else if ([postValue isKindOfClass:[UIImage class]]) {
                [postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@.png\"\r\n", key, [TSGUIDCreator generateGUID]] dataUsingEncoding:NSUTF8StringEncoding]];
                [postData appendData:[@"Content-Type: image/jpeg\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
                [postData appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
                [postData appendData:UIImagePNGRepresentation(postValue)];
            } else {
                [NSException raise:@"Invalid Post Value" format:@"Received invalid post value while trying to create URL Request. Post values are required to be strings. The value for the following key was not a string: %@.", key];
            }
        }

        [postData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    }

    //setup the request
    [urlRequest setURL:[NSURL URLWithString:URL]];
    [urlRequest setHTTPMethod:@"POST"];
    [urlRequest setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary] forHTTPHeaderField:@"Content-Type"];
    [urlRequest setHTTPBody:postData];

    return urlRequest;
}
于 2013-01-09T05:00:36.743 回答