网址请求表
我有这个 html 表单将我的数据从 iphone 传递到网络服务器。 但我一直不知道如何将此表单/数据构建到可变请求中。你可以吗?告诉我。
Html 表单:
<html>
<form method="post" action="https://mysite.com">
<input type="hidden" name="action" value="sale">
<input type="hidden" name="acctid" value="TEST123">
<input type="hidden" name="amount" value="1.00">
<input type="hidden" name="name" value="Joe Customer">
<input type="submit">
</form>
</html>
我不知道如何在 url 请求中将“值”分配给特定键(例如操作、acctid、金额、名称)???
这是我的代码:
NSString *urlString = @"https://mysite.com";
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
NSString *post = [[NSString alloc] initWithFormat:@"%@%@&%@%@&%@%@&%@%@",
action, sale,
acctid, TEST123,
amount, 1.00,
name, Joe Customer]; // ????
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[urlRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; // multipart/form-data
[urlRequest setHTTPBody:postData];
I have this html form to pass my data from iphone to web server..
but I've got stucked how to build this form/data into a mutable request. Can you pls. advise me.
Html form:
<html>
<form method="post" action="https://mysite.com">
<input type="hidden" name="action" value="sale">
<input type="hidden" name="acctid" value="TEST123">
<input type="hidden" name="amount" value="1.00">
<input type="hidden" name="name" value="Joe Customer">
<input type="submit">
</form>
</html>
I don't know how to assign in the url request the "value" to the specific key (ex. action, acctid, amount, name) ???
This is my code:
NSString *urlString = @"https://mysite.com";
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
NSString *post = [[NSString alloc] initWithFormat:@"%@%@&%@%@&%@%@&%@%@",
action, sale,
acctid, TEST123,
amount, 1.00,
name, Joe Customer]; // ????
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[urlRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; // multipart/form-data
[urlRequest setHTTPBody:postData];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来不错,尽管格式字符串中缺少一些等号,并且需要将字符串参数包装在
@""
中:为了获得更具可扩展性的解决方案,您可以存储键/值字典中的对然后执行如下操作:(
请记住,您可能需要对键和值进行 URL 编码。)
Looks about right, although you're missing some equals signs in your format string, and you need to wrap your string arguments up in
@""
:For a more extendable solution you could store your key/value pairs in a dictionary then do something like this:
(Bear in mind you may need to URL-encode the keys and values.)