加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 05:38:32 所属栏目:百科 来源:网络整理
导读:我正在使用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" valu
我正在使用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];

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读