iOS学习总结之网络编程
2015-08-30 20:53
435 查看
今天刚刚自学完网络请求,现将两种请求方式总结如下
所有网络请求 统一使用异步请求
GET请求
1、URL
2、NSURLRequest
3、NSURLConnction
异步
自己搭建的本地服务器 ,没有安装服务器的可以安装一下,方便练习
POST请求
1、URL
2、NSMutableURLRequest
.httpMethod = @"POST";
.HTTPBody = [str dataUsingEncoding:NSUTF8StringEncoding];
3、NSURLConnction
异步
所有网络请求 统一使用异步请求
GET请求
1、URL
2、NSURLRequest
3、NSURLConnction
异步
自己搭建的本地服务器 ,没有安装服务器的可以安装一下,方便练习
#pragma mark-GET登录 - (void)getLogin{ // 1、URL NSString *urlStr = [NSString stringWithFormat:@"http://localhost/login.php?username=%@&password=%@", self.userName.text, self.userPsw.text]; NSURL *url = [NSURL URLWithString:urlStr]; //2、request NSURLRequest *request = [NSURLRequest requestWithURL:url]; [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { if (connectionError == nil) { // 将Data转成字符串 NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; // 更新界面 [[NSOperationQueue mainQueue] addOperationWithBlock:^{ NSLog(@"登录完成"); }]; } }];
POST请求
1、URL
2、NSMutableURLRequest
.httpMethod = @"POST";
.HTTPBody = [str dataUsingEncoding:NSUTF8StringEncoding];
3、NSURLConnction
异步
#pragma mark -POST登录 - (void)postLogin{ NSURL *url = [NSURL URLWithString:@"http://localhost/login.php"]; //请求(可变请求) NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; //默认是get请求 request.HTTPMethod = @"POST"; //数据体 NSString *str = [NSString stringWithFormat:@"username=%@&password=%@", self.userName.text, self.userPsw.text]; //将字符串转换成数据 request.HTTPBody = [str dataUsingEncoding:NSUTF8StringEncoding]; //连接 异步 [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { if (connectionError == nil) { //将Data转成字符串 NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; //更新界面 [[NSOperationQueue mainQueue] addOperationWithBlock:^{ NSLog(@"%@",str); }]; } }]; }
相关文章推荐
- Android4.0以上版本Http请求的问题
- Volley理解
- [网络转载 ]LoadRunner技巧之THML与URL两种录制模式分析
- 网络协议
- configure: error: in `/root/httpd-2.2.11/srclib/apr': c
- HTTP API选择WinInet or WinHttp?
- TCP/IP协议详解内容总结(怒喷一口老血)
- 计算机网络知识点
- 强制使用处于TIME_WAIT的TCP状态的socket地址
- Atitit.request http乱码的设计防止 检测与解决最近实践p825 attilax总结.doc
- Atitit.request http乱码的设计防止 检测与解决最近实践p825 attilax总结.doc
- Atitit.request http乱码的设计防止 检测与解决最近实践p825 attilax总结.doc
- oracle网络配置listener.ora、sqlnet.ora、tnsnames.ora
- ios Communicating with HTTP Servers 与HTTP服务器通信 官方文档翻译(五)
- SCADA组态系统将主宰新的网络安全领域
- HTTP Get,Post请求详解
- .net学习笔记---HttpRuntime类
- 基本TCP套接字编程
- .net学习笔记---HttpResponse类
- .net学习笔记----HttpRequest类