I'm using AFNetworking framework and need to submit a form (POST) request to the server. Here's a sample of what server expects:
我正在使用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>
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.
我考虑在AFHTTPClient中使用multipartFormRequestWithMethod,就像在post中使用AFNetworking发送多个图像时所讨论的那样。但我不知道如何使用“radio”类型输入值附加表单数据。
3 个解决方案
#1
22
Here's an example using NSURLConnection to send POST parameters:
以下是使用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];
#2
5
If you take a look at what the browser submits to the server when using this form (using the debug panel in your browser) you'd see that your POST request data looks like this:
如果您在使用此表单(使用浏览器中的调试面板)时查看浏览器提交给服务器的内容,您会看到POST请求数据如下所示:
paperid=6&q77=2&q80=blah
That is the value entry from the selected radio button is used as the value for the corresponding POST entry, and you get just one entry for all of the radio buttons. (As opposed to toggle buttons, where you get a value for each that is currently selected.)
也就是说,所选单选按钮的值条目用作相应POST条目的值,并且您只获得所有单选按钮的一个条目。 (与切换按钮相反,您可以在其中获取当前所选每个的值。)
Once you understand the format of the POST string you should be able to create the request in the usual manner using ASIFormDataRequest.
一旦理解了POST字符串的格式,就应该能够使用ASIFormDataRequest以通常的方式创建请求。
#3
2
Here is how to do with STHTTPRequest
以下是如何处理STHTTPRequest
STHTTPRequest *r = [STHTTPRequest requestWithURLString:@"http://www.whereq.com:8079/answer.m"];
r.POSTDictionary = @{ @"paperid":@"6", @"q77":"1", @"q80":@"hello" };
r.completionBlock = ^(NSDictionary *headers, NSString *body) {
// ...
};
r.errorBlock = ^(NSError *error) {
// ...
};
[r startAsynchronous];
#1
22
Here's an example using NSURLConnection to send POST parameters:
以下是使用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];
#2
5
If you take a look at what the browser submits to the server when using this form (using the debug panel in your browser) you'd see that your POST request data looks like this:
如果您在使用此表单(使用浏览器中的调试面板)时查看浏览器提交给服务器的内容,您会看到POST请求数据如下所示:
paperid=6&q77=2&q80=blah
That is the value entry from the selected radio button is used as the value for the corresponding POST entry, and you get just one entry for all of the radio buttons. (As opposed to toggle buttons, where you get a value for each that is currently selected.)
也就是说,所选单选按钮的值条目用作相应POST条目的值,并且您只获得所有单选按钮的一个条目。 (与切换按钮相反,您可以在其中获取当前所选每个的值。)
Once you understand the format of the POST string you should be able to create the request in the usual manner using ASIFormDataRequest.
一旦理解了POST字符串的格式,就应该能够使用ASIFormDataRequest以通常的方式创建请求。
#3
2
Here is how to do with STHTTPRequest
以下是如何处理STHTTPRequest
STHTTPRequest *r = [STHTTPRequest requestWithURLString:@"http://www.whereq.com:8079/answer.m"];
r.POSTDictionary = @{ @"paperid":@"6", @"q77":"1", @"q80":@"hello" };
r.completionBlock = ^(NSDictionary *headers, NSString *body) {
// ...
};
r.errorBlock = ^(NSError *error) {
// ...
};
[r startAsynchronous];