Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何在iOS上模拟HTTP表单(POST)提交_Objective C_Ios_Afnetworking - Fatal编程技术网

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

Objective c 如何在iOS上模拟HTTP表单(POST)提交,objective-c,ios,afnetworking,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">

我正在使用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中使用MultPuffFraseSuffEdvices方法,就像在POST中讨论的一样。但是我不知道如何使用“radio”类型的输入值附加表单数据。

如果您查看使用此表单时浏览器提交给服务器的内容(使用浏览器中的调试面板),您将看到您的POST请求数据如下所示:

paperid=6&q77=2&q80=blah
也就是说,来自所选单选按钮的值条目被用作相应POST条目的值,并且您只会为所有单选按钮获得一个条目。(与切换按钮相反,切换按钮可为当前选定的每个按钮获取一个值。)


一旦您了解了POST字符串的格式,您就应该能够使用通常的方式创建请求

以下是使用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];
这里是如何处理的


看起来您的服务器期望的示例没有实现。此外,我还可以提供一个示例,说明如果您不喜欢AFNetworking,如何使用NSURLConnection实现这一点……嗨,obuseme,请提供示例。我不介意使用NSURLConnection和NSMUTABLeRequest解决了这个问题,除了添加一些POST请求参数之外没什么特别的。Chris,我很好奇你添加了什么,因为我已经尝试了一段时间了。我使用了一段web代码,其中有一个“action”表单参数(就像你的示例那样),一个“email”输入和另一个“密码”,将我的URL设置为“操作”URL,然后使用上面的添加电子邮件/密码值(UUEncoded),然后,它第一次工作!但愿我能给你双倍的选票!如果其他字段是对象呢?i、 e.NSString*postString=@“paperid=6&q77=2&q80=blah&obj=??”你们是怎么处理的?这不好,因为它不使用postBody,在服务器端获取数组。数组([q80]=>hello[paperid]=>6[q77]=>1)如何发送没有参数名的对象?
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];