GET和POST
来源:互联网 发布:c语言表达式的级别 编辑:程序博客网 时间:2024/05/22 01:34
GET
// 1. URL NSString *urlStr = [NSString stringWithFormat:@"http://localhost/login.php?username=%@&password=%@", self.userName.text, self.userPwd.text]; NSURL *url = [NSURL URLWithString:urlStr]; // 2. Request NSURLRequest *request = [NSURLRequest requestWithURL:url]; // 3. Connection // 1> 登录完成之前,不能做后续工作! // 2> 登录进行中,可以允许用户干点别的会更好! // 3> 让登录操作在其他线程中进行,就不会阻塞主线程的工作 // 4> 结论:登陆也是异步访问,中间需要阻塞住 [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]; // num = 2 NSLog(@"%@ %@", str, [NSThread currentThread]); // 更新界面 [[NSOperationQueue mainQueue] addOperationWithBlock:^{ self.logonResult.text = @"登录完成"; }]; } }]; // num = 1 NSLog(@"come here %@", [NSThread currentThread]); NSURLResponse *response = nil; // 1. &response真的理解了吗? // 2. error:为什么是NULL,而不是nil // NULL是C语言的 = 0 // 在C语言中,如果将指针的地址指向0就不会有危险 // nil是OC的,是一个空对象发送消息不会出问题// [response MIMEType]; [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:NULL];
POST
<pre name="code" class="objc"> // 1. URL NSURL *url = [NSURL URLWithString:@"http://localhost/login.php"]; // 2. 请求(可以改的请求) NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; // ? POST // 默认就是GET请求 request.HTTPMethod = @"POST"; // ? 数据体 NSString *str = [NSString stringWithFormat:@"username=%@&password=%@", self.userName.text, self.userPwd.text]; // 将字符串转换成数据 request.HTTPBody = [str dataUsingEncoding:NSUTF8StringEncoding]; // 3. 连接,异步 [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]; // num = 2 NSLog(@"%@ %@", str, [NSThread currentThread]); // 更新界面 [[NSOperationQueue mainQueue] addOperationWithBlock:^{ self.logonResult.text = str; }]; } }]; // num = 1 NSLog(@"come here %@", [NSThread currentThread]);
1 0
- Post和Get的
- post和get传值
- GET和POST
- GET和POST
- get 和 post
- 区分get和post
- GET 和 POST 区别
- 关于GET和POST
- get 和 post
- Get和Post区别
- get和post区别
- get和post
- GET和POST区别
- Get 和 post区别
- get和post
- get和post区别
- POST和GET区别
- C# Get和Post
- 顺时针打印矩阵
- 将Data转换成字符串
- 优秀程序员所具备的7个好习惯
- Ubuntu下安装eclipse,打开时出现A Java RunTime Environment (JRE) or Java Development Kit (JDK)。。。
- C++,二叉树,叶子节点
- GET和POST
- pair 与 make_pair
- SQLSERVER如何获取一个数据库中的所有表的名称、一个表中所有字段的名称
- 去除字符串中的空格
- Java环境变量配置记录
- [转]VC获取屏幕分辨率及大小相关
- jQuery第三天动画效果 、 jQuery类数组操作小结(初级)
- java中的多线程——线程并发库
- c++初涉