Objective-C 中的简单 http post 示例? [英] Simple http post example in Objective-C?
问题描述
我有一个需要登录的 php 网页(用户 ID 和密码).我让用户将信息输入应用程序就好了..但我需要一个关于如何向网站发出 POST 请求的示例.支持网站上的苹果示例显示图片上传相当复杂..我的应该更简单..我只想发布2行文字..谁有好的例子?
I have a php webpage that requires a login (userid & password). I have the user enter the information into the app just fine.. but I need an example on how to do a POST request to a website. The apple example on the support site is rather complicated showing a picture upload.. mine should be simpler.. I just want to post 2 lines of text.. Anyone have any good examples?
亚历克斯
推荐答案
这是我最近使用的,对我来说效果很好:
This is what I recently used, and it worked fine for me:
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,但那个博客似乎已经不存在了.
Originally taken from http://deusty.blogspot.com/2006/11/sending-http-get-and-post-from-cocoa.html, but that blog does not seem to exist anymore.
这篇关于Objective-C 中的简单 http post 示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!