我正在使用 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/