您的位置:首页 > 理论基础 > 计算机网络

ios中http的两种请求方法:get与post

2013-01-16 14:26 387 查看
get主要负责查询,安全性低,操作方便;

post安全性高,操作如下:

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
//设置提交目的url
[request setURL:[NSURL URLWithString:kUserLoginCheckUrl]];
//设置提交方式为 POST
[request setHTTPMethod:@"POST"];
//设置http-header:Content-Type
//这里设置为 application/x-www-form-urlencoded ,如果设置为其它的,比如text/html;charset=utf-8,或者 text/html 等,都会出错。不知道什么原因。
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
//设置http-header:Content-Length
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
//设置需要post提交的内容
[request setHTTPBody:postData];

//定义
NSHTTPURLResponse* urlResponse = nil;
NSError *error = [[NSError alloc] init];
//同步提交:POST提交并等待返回值(同步),返回值是NSData类型。
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
这个基本就是完整的请求过程
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: