objective-c - Objective-C 中的简单 http post 示例?

我有一个需要登录(用户名和密码)的 php 网页。我让用户将信息输入到应用程序中就好了..但我需要一个关于如何向网站发出 POST 请求的示例。支持网站上的苹果示例显示图片上传相当复杂..我的应该更简单..我只想发布2行文字.. 谁有好的例子?

亚历克斯

最佳答案

这是我最近使用的,对我来说效果很好:

NSString *post = @"key1=val1&key2=val2";
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"http://www.nowhere.com/sendFormHere.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

原文取自 http://deusty.blogspot.com/2006/11/sending-http-get-and-post-from-cocoa.html ,但那个博客似乎已经不存在了。

https://stackoverflow.com/questions/3566516/

相关文章:

objective-c - 从 NSMutableString 中删除最后一个字符

iphone - 以流畅的动画显示/隐藏导航栏

objective-c - 对 NSTimer 目标的弱引用以防止保留循环

objective-c - 如何在 Objective-C 中使用自定义委托(delegate)

c - 什么是双星(例如 NSError **)?

objective-c - 可变集合有文字语法吗?

ios - 如何调整 UITableView 中原型(prototype)单元格的左边距?

ios - 缩放 UIView 及其所有子项

objective-c - Xcode 在 iOS 8 的 Main() 中抛出异常,断点为 'al

objective-c - iOS 8 Mapkit Objc 无法获取用户位置