如何在目标c(包含工作卷曲行)中执行特定的HTTP POST?

我的API需要通过HTTP POST接收一些参数和图像。 在卷曲它完成与:

curl http://someurl -F 'service[lat]=12.22' -F 'sevice[lng]=12.33' -F 'service[user_id]=4' -F 'service[location]=Vienna' -F 'service[image]=@tour.jpg'

如何在目标C(iphone)中完成? 我想这是在内容类型设置为“application / x-www-form-urlencoded”的情况下完成的?


NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"random string of your choosing";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

NSMutableData *body = [NSMutableData data];

// file
[body appendData:[[NSString stringWithFormat:@"rn--%@rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="name"; filename="%@"rn", fileName] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-streamrnrn" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[@"rn" dataUsingEncoding:NSUTF8StringEncoding]];

// length
[body appendData:[[NSString stringWithFormat:@"--%@rn", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="size"rnrn"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%i",imageData.length] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"rn" dataUsingEncoding:NSUTF8StringEncoding]];

// lat
[body appendData:[[NSString stringWithFormat:@"--%@rn", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="lat"rnrn"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%f",lat] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"rn" dataUsingEncoding:NSUTF8StringEncoding]];    

// lon
[body appendData:[[NSString stringWithFormat:@"--%@rn", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="lon"rnrn"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%f",lon] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"rn" dataUsingEncoding:NSUTF8StringEncoding]];

// location
[body appendData:[[NSString stringWithFormat:@"--%@rn", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="location"rnrn"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:location] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"rn" dataUsingEncoding:NSUTF8StringEncoding]];

// user_id
[body appendData:[[NSString stringWithFormat:@"--%@rn", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="user_id"rnrn"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:user_id] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"rn" dataUsingEncoding:NSUTF8StringEncoding]];

// close form
[body appendData:[[NSString stringWithFormat:@"--%@--rn", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

// set request body
[request setHTTPBody:body];

//bon voyage
[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

我建议你看看ASIHTTPRequest框架,如果你想避免自己构建发布数据的麻烦。

来自文档的示例:

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addPostValue:@"Ben" forKey:@"names"];
[request addPostValue:@"George" forKey:@"names"];
[request addFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photos"];
[request addData:imageData withFileName:@"george.jpg" andContentType:@"image/jpeg" forKey:@"photos"];

我在我的所有项目中都使用它,它非常强大且易于使用。


请不要使用ASIHTTPRequest。 从开发者的主页:

请注意,我不再在这个图书馆工作 - 你可能想考虑使用别的东西来做新的项目。 :)

我听说过有关AFNetworking的好消息

但是通过NSURLRequest实现POST请求并不困难,你不需要依赖另一个库。

链接地址: http://www.djcxy.com/p/8615.html

上一篇: How to do specific HTTP POST in objective c (working curl line included)?

下一篇: Curl setting Content