我使用以下方法创建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
中的值。
我的问题是如何修改此代码以发送其他类型的值?具体来说,我正在尝试将图像发送到服务器。
答案 0 :(得分: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/
答案 1 :(得分:1)
您应该查看this tutorial。
基本上你需要
答案 2 :(得分: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;
}