如何在iPhone中的URL中发送多个帖子数据

发布于 2024-12-02 15:17:29 字数 871 浏览 1 评论 0原文

目前我知道我可以通过 HTTP 请求将 post 数据发送到 URL。有没有办法通过 HTTP 请求发送附加到 URL 的多个帖子数据。我知道我可以发送 POST 数据,因为我一直在这样做,但不知道如何附加多个帖子数据。 下面是我的单个帖子的代码。有没有办法将另一个 NSString key2 添加到 URL 中?

NSString *initialURL = [NSString stringWithFormat:@"http://www.abc.com"];
NSURL *url=[NSURL URLWithString:initialURL];

NSString *key = [NSString stringWithFormat:@"key=%@", keyValue];
NSData *mastData = [key dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *mastLength = [NSString stringWithFormat:@"%d",[mastData length]];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:mastLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:mastData];

Currently I know I can send post data to URL through HTTP request. Is there a way to send multiple post data attached to the URL through HTTP request. I know for the fact that I can send POST data since I have been doing it but no idea on how to append multiple post data.
Below is my code for single post. Is there a way to add another NSString key2 to the URL ?

NSString *initialURL = [NSString stringWithFormat:@"http://www.abc.com"];
NSURL *url=[NSURL URLWithString:initialURL];

NSString *key = [NSString stringWithFormat:@"key=%@", keyValue];
NSData *mastData = [key dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *mastLength = [NSString stringWithFormat:@"%d",[mastData length]];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:mastLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:mastData];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月亮坠入山谷 2024-12-09 15:17:29

您可以扩展它

NSString *key = [NSString stringWithFormat:@"key=%@", keyValue];

并添加更多由 &s 分隔的键值对,如下所示

NSString *key = [NSString stringWithFormat:@"key=%@&key2=%2", keyValue, key2value];

You can extend this

NSString *key = [NSString stringWithFormat:@"key=%@", keyValue];

and add more key value pairs separated by &s, like this

NSString *key = [NSString stringWithFormat:@"key=%@&key2=%2", keyValue, key2value];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文