IOS网络第二天 - 06-POST请求
2015-09-14 16:21
344 查看
************POST请求
#import "HMViewController.h" #import "MBProgressHUD+MJ.h" @interface HMViewController () @property (weak, nonatomic) IBOutlet UITextField *username; @property (weak, nonatomic) IBOutlet UITextField *pwd; - (IBAction)login; @end @implementation HMViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.view endEditing:YES]; } - (IBAction)login { // 1.用户名 NSString *usernameText = self.username.text; if (usernameText.length == 0) { [MBProgressHUD showError:@"请输入用户名"]; return; } // 2.密码 NSString *pwdText = self.pwd.text; if (pwdText.length == 0) { [MBProgressHUD showError:@"请输入密码"]; return; } // 增加蒙板 [MBProgressHUD showMessage:@"正在拼命登录中...."]; // 3.发送用户名和密码给服务器(走HTTP协议) // 创建一个URL : 请求路径 NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/login"]; // 创建一个请求 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; // 5秒后算请求超时(默认60s超时) request.timeoutInterval = 15; request.HTTPMethod = @"POST"; // 设置请求体 NSString *param = [NSString stringWithFormat:@"username=%@&pwd=%@", usernameText, pwdText]; // NSString --> NSData request.HTTPBody = [param dataUsingEncoding:NSUTF8StringEncoding]; // 设置请求头信息 [request setValue:@"iPhone 6" forHTTPHeaderField:@"User-Agent"]; // 发送一个同步请求(在主线程发送请求) // queue :存放completionHandler这个任务 NSOperationQueue *queue = [NSOperationQueue mainQueue]; [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler: ^(NSURLResponse *response, NSData *data, NSError *connectionError) { // 隐藏蒙板 [MBProgressHUD hideHUD]; // NSHTTPURLResponse *resp = (NSHTTPURLResponse *)response; // NSString *msg = [NSHTTPURLResponse localizedStringForStatusCode:resp.statusCode]; // NSLog(@"%d %@ %@", resp.statusCode, msg, resp.allHeaderFields); // 这个block会在请求完毕的时候自动调用 if (connectionError || data == nil) { // 一般请求超时就会来到这 [MBProgressHUD showError:@"请求失败"]; return; } // 解析服务器返回的JSON数据 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; NSString *error = dict[@"error"]; if (error) { // {"error":"用户名不存在"} // {"error":"密码不正确"} [MBProgressHUD showError:error]; } else { // {"success":"登录成功"} NSString *success = dict[@"success"]; [MBProgressHUD showSuccess:success]; } }]; } @end
相关文章推荐
- IOS网络第二天 - 05-黑酷-NSXMLparser解析(略)
- IOS网络第二天 - 04-黑酷-GDataXML 解析
- SGU326Perspective(网络流量的最大流量)(经典赛车模型)
- IOS网络第二天 - 03-JSON显示数据,调用本地视频播放,数据转模型
- Wireshark使用方法(学习笔记二——查询统计) http://www.2cto.com/Article/201201/115880.html
- android网络框架的封装——volley
- Live555用做RTSPClient时,利用RTP时间戳进行音视频同步的解决方案(必须有RTCP支持才可行)
- 浅谈 Linux 内核开发之网络设备驱动
- 【网络通信:Volley】请求的发送与响应之String
- 计算机网络--NAT
- HTTPS与SSL详解(二)
- HTTPS与SSL详解(一)
- 理解strong与weak)强引用与弱引用转载自《http://blog.sina.com.cn/s/blog_7c8dc2d50101lc08.html》
- JAVA: httpclient 详细说明——第四章;
- 一文读懂卷积神经网络
- Nginx Http 核心模块中Server Location 配置
- VM虚拟机与宿主机网络互联的方法
- 【网络通信:Volley】Volley开篇
- Linux系统下修改网络配置
- Android 网络通信框架Volley