如何在iOS上模拟HTTP表单(POST)提交 [英] How to simulate HTTP form (POST) submit on iOS

查看:2549
本文介绍了如何在iOS上模拟HTTP表单(POST)提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using AFNetworking framework and need to submit a form (POST) request to the server. Here's a sample of what server expects:

<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,就像在post 使用AFNetworking发送多个图像。但我不知道如何使用radio类型输入值附加表单数据。

I consider using the multipartFormRequestWithMethod in AFHTTPClient, just like what was discussed in post Sending more than one image with AFNetworking. But I have no idea about how to append the form data with "radio" type input value.

推荐答案

这是使用NSURLConnection的示例发送POST参数:

Here's an example using NSURLConnection to send POST parameters:

// 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];

这篇关于如何在iOS上模拟HTTP表单(POST)提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆