使用 ASIHTTPRequest POST 多部分/表单数据
我是 ASIHTTPRequest 的新手,在实现表单时遇到问题。 目前,我在 UIWebForm 中使用以下代码,一切正常。
NSString *body = @"\
<html> \
<head> \
<meta http-equiv=\"Content-Type\" content=\"text/html;
charset=iso-8859-1\"> \
</head> \
<body>\
<form name=\"my_form\" action=\"http://www.server.com/ar?
ca=11_s&id=22630957\" \
method=\"POST\" enctype=\"multipart/form-data\"> \
<input type=\"text\" name=\"name\" value=\"myname\"> \
<input type=\"text\" name=\"email\" value=\"[email protected]\"> \
<textarea name=\"body\">My message</textarea> \
<input type=checkbox name=\"sendercc\" value=\"0\"> \
<input type=\"submit\" name=\"send\" value=\"Send\"> \
</table> \
</form> \
</body>\
</html>";
应替换表单的(不起作用)代码如下:
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:
[[[NSURL alloc] initWithString:@"http://www.server.com/ar?
ca=11_s&id=22630957"] autorelease]];
[request setResponseEncoding:NSISOLatin1StringEncoding];
[request setPostFormat:ASIMultipartFormDataPostFormat];
[request setPostValue:@"myname" forKey:@"name"];
[request setPostValue:@"[email protected]" forKey:@"email"];
[request setPostValue:@"My message" forKey:@"body"];
[request setPostValue:@"0" forKey:@"sendercc"];
[request startSynchronous];
I'm new to ASIHTTPRequest and I have a problem to implement a form.
At the moment I use the following code inside a UIWebForm and everything work fine.
NSString *body = @"\
<html> \
<head> \
<meta http-equiv=\"Content-Type\" content=\"text/html;
charset=iso-8859-1\"> \
</head> \
<body>\
<form name=\"my_form\" action=\"http://www.server.com/ar?
ca=11_s&id=22630957\" \
method=\"POST\" enctype=\"multipart/form-data\"> \
<input type=\"text\" name=\"name\" value=\"myname\"> \
<input type=\"text\" name=\"email\" value=\"[email protected]\"> \
<textarea name=\"body\">My message</textarea> \
<input type=checkbox name=\"sendercc\" value=\"0\"> \
<input type=\"submit\" name=\"send\" value=\"Send\"> \
</table> \
</form> \
</body>\
</html>";
The (not working) code that should replace the form is the follow:
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:
[[[NSURL alloc] initWithString:@"http://www.server.com/ar?
ca=11_s&id=22630957"] autorelease]];
[request setResponseEncoding:NSISOLatin1StringEncoding];
[request setPostFormat:ASIMultipartFormDataPostFormat];
[request setPostValue:@"myname" forKey:@"name"];
[request setPostValue:@"[email protected]" forKey:@"email"];
[request setPostValue:@"My message" forKey:@"body"];
[request setPostValue:@"0" forKey:@"sendercc"];
[request startSynchronous];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您能详细解释一下它是如何不起作用的吗?如果检查 request.responseString,是否会从服务器返回错误?
您可能需要为提交按钮添加一个值,即:
如果这没有帮助,请尝试使用代理,例如 CharlesProxy< /a> 捕获来自浏览器的工作请求和非工作 iPhone 请求,并比较它们以查找任何差异。
Could you explain more about how it doesn't work? Do you get an error back from the server if you check request.responseString?
You may need to add a value for the submit button, ie:
If that doesn't help, try using a proxy like CharlesProxy to capture both a working request from the browser and the non-working iphone request, and compare them looking for any differences.