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 に答える