objective-c - 如何在 iOS 上模拟 HTTP 表单(POST)提交

标签 objective-c ios afnetworking

我正在使用 AFNetworking 框架,需要向服务器提交表单 (POST) 请求。这是服务器期望的示例:

<form id="form1" method="post" action="http://www.whereq.com:8079/answer.m">
    <input type="hidden" name="paperid" value="6">
    <input type="radio" name="q77" value="1">
    <input type="radio" name="q77" value="2">
    <input type="text" name="q80">
</form> 

我考虑在 AFHTTPClient 中使用 multipartFormRequestWithMethod,就像帖子 Sending more than one image with AFNetworking 中讨论的那样.但是我不知道如何使用“radio”类型的输入值附加表单数据。

最佳答案

下面是一个使用 NSURLConnection 发送 POST 参数的例子:

// Note that the URL is the "action" URL parameter from the form.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.whereq.com:8079/answer.m"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
//this is hard coded based on your suggested values, obviously you'd probably need to make this more dynamic based on your application's specific data to send
NSString *postString = @"paperid=6&q77=2&q80=blah";
NSData *data = [postString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];
[request setValue:[NSString stringWithFormat:@"%u", [data length]] forHTTPHeaderField:@"Content-Length"];
[NSURLConnection connectionWithRequest:request delegate:self];

关于objective-c - 如何在 iOS 上模拟 HTTP 表单(POST)提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12658724/

相关文章:

ios - Google Maps URL Scheme 不显示标记

ios - View Controller 、依赖注入(inject)和 init/viewDidLoad

iOS - 如何在 UIViewController 中的首选位置显示自定义弹出 View

ios - Xcode 快速删除按钮

iphone - AFNetworking:返回 200 且无内容时运行失败 block

iphone - 使用 AFNetworking 下载大文件

ios - 使用 AFNetworking 上传简单的 "PUT"文件

objective-c - iPad 键盘方向错误

ios - 如何使用 Objective-c 为 iPhone/iPad 生成设备的唯一 ID

iphone - 在 iPhone 应用程序中计费