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];
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];
相关文章推荐
- Linux命令发送Http的get或post请求(curl和wget两种方法)
- Linux 下模拟Http 的get or post请求(curl和wget两种方法)
- 两种 HTTP 请求方法:GET 和 POST
- Linux命令发送Http的get或post请求(curl和wget两种方法)
- IOS 请求服务器的两种方法GET 和 POST
- [置顶] 两种HTTP请求方法:GET和POST的区别
- linux命令发送http的get或post请求(curl和wget两种方法)
- 面试2-两种 HTTP 请求方法:GET 和 POST
- 两种 HTTP 请求方法:GET 和 POST
- 两种HTTP请求方法:GET和POST
- Linux 下模拟Http 的get or post请求(curl和wget两种方法)
- Linux命令发送Http的get或post请求(curl和wget两种方法)
- 两种 HTTP 请求方法:GET 和 POST
- http中post 和 get 请求方法区别大全
- HTTP协议中POST、GET、HEAD、PUT等请求方法以及一些常见错误
- HTTP协议中POST、GET、HEAD、PUT等请求方法以及一些常见错误
- Android HttpClient GET或者POST请求基本使用方法
- iOS 网络编程(HTTP 同步GET请求,同步POST请求,异步GET请求,异步POST请求)
- HTTP 协议请求方法GET 与POST 的区别?