我有这个 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 请求中将“值”分配给特定键(例如 action、acctid、amount、name)???
这是我的代码:
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];