码迷,mamicode.com
首页 > 其他好文 > 详细

Get&Post登录

时间:2015-12-19 23:09:38      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:

  1 #import "MJViewController.h"
  2 
  3 @interface MJViewController ()
  4 @property (weak, nonatomic) IBOutlet UITextField *userName;
  5 @property (weak, nonatomic) IBOutlet UITextField *userPwd;
  6 
  7 @property (weak, nonatomic) IBOutlet UILabel *logonResult;
  8 
  9 @end
 10 
 11 @implementation MJViewController
 12 /**
 13  所有网络请求,统一使用异步请求!
 14  
 15  在今后的开发中,如果使用简单的get/head请求,可以用NSURLConnction异步方法
 16  GET查/POST增/PUT改/DELETE删/HEAD
 17  
 18  GET
 19  1> URL
 20  2> NSURLRequest
 21  3> NSURLConnction 异步
 22  
 23  POST
 24  1> URL
 25  2> NSMutableURLRequest
 26     .HTTPMethod = @"POST";
 27     str 从 firebug直接粘贴,或者自己写 
 28     变量名1=数值1&变量名2=数值2
 29  
 30     .HTTPBody  = [str dataUsingEncoding:NSUTF8StringEncoding];
 31  3> NSURLConnction 异步
 32  
 33  */
 34 - (IBAction)userLogon
 35 {
 36     [self postLogon];
 37 }
 38 
 39 #pragma mark - POST登录
 40 - (void)postLogon
 41 {
 42     // 1. URL
 43     NSURL *url = [NSURL URLWithString:@"http://localhost/login.php"];
 44     
 45     // 2. 请求(可以改的请求)
 46     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
 47     // ? POST
 48     // 默认就是GET请求
 49     request.HTTPMethod = @"POST";
 50     // ? 数据体
 51     NSString *str = [NSString stringWithFormat:@"username=%@&password=%@", self.userName.text, self.userPwd.text];
 52     // 将字符串转换成数据
 53     request.HTTPBody = [str dataUsingEncoding:NSUTF8StringEncoding];
 54     
 55     // 3. 连接,异步
 56     [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
 57         
 58         if (connectionError == nil) {
 59             // 网络请求结束之后执行!
 60             // 将Data转换成字符串
 61             NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
 62             
 63             // num = 2
 64             NSLog(@"%@ %@", str, [NSThread currentThread]);
 65             
 66             // 更新界面
 67             [[NSOperationQueue mainQueue] addOperationWithBlock:^{
 68                 self.logonResult.text = str;
 69             }];
 70         }
 71     }];
 72     
 73     // num = 1
 74     NSLog(@"come here %@", [NSThread currentThread]);
 75 }
 76 
 77 #pragma mark - GET登录
 78 - (void)getLogon
 79 {
 80     // 1. URL
 81     NSString *urlStr = [NSString stringWithFormat:@"http://localhost/login.php?username=%@&password=%@", self.userName.text, self.userPwd.text];
 82     
 83     NSURL *url = [NSURL URLWithString:urlStr];
 84     
 85     // 2. Request
 86     NSURLRequest *request = [NSURLRequest requestWithURL:url];
 87     
 88     // 3. Connection
 89     // 1> 登录完成之前,不能做后续工作!
 90     // 2> 登录进行中,可以允许用户干点别的会更好!
 91     // 3> 让登录操作在其他线程中进行,就不会阻塞主线程的工作
 92     // 4> 结论:登陆也是异步访问,中间需要阻塞住
 93     [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
 94         
 95         if (connectionError == nil) {
 96             // 网络请求结束之后执行!
 97             // 将Data转换成字符串
 98             NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
 99             
100             // num = 2
101             NSLog(@"%@ %@", str, [NSThread currentThread]);
102             
103             // 更新界面
104             [[NSOperationQueue mainQueue] addOperationWithBlock:^{
105                 self.logonResult.text = @"登录完成";
106             }];
107         }
108     }];
109     
110     // num = 1
111     NSLog(@"come here %@", [NSThread currentThread]);
112     
113     NSURLResponse *response = nil;
114     // 1. &response真的理解了吗?
115     // 2. error:为什么是NULL,而不是nil
116     // NULL是C语言的 = 0
117     // 在C语言中,如果将指针的地址指向0就不会有危险
118     
119     // nil是OC的,是一个空对象发送消息不会出问题
120 //    [response MIMEType];
121     [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:NULL];
122 }
123 
124 @end

 

Get&Post登录

标签:

原文地址:http://www.cnblogs.com/liuguan/p/5059915.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!