在Objective-C中简单的http post示例? [英] Simple http post example in Objective-C?

查看:704
本文介绍了在Objective-C中简单的http post示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要登录(用户名和密码)的php网页。我有用户输入信息进入应用程序很好..但我需要一个例子,如何做一个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?

Alex

推荐答案

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
这是我最近使用的,它对我来说很好。

Taken from http://deusty.blogspot.com/2006/11/sending-http-get-and-post-from-cocoa.html . Thats what I recently used, and it worked fine for me.

Btw,5秒的Google搜索会得到相同的结果, post request cocoa;)

Btw, 5 seconds of googling would've let to the same result, using the terms "post request cocoa" ;)

这篇关于在Objective-C中简单的http post示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆