get-post请求
来源:互联网 发布:windows ubuntu 16.04 编辑:程序博客网 时间:2024/06/07 02:37
Single View Application
ViewController.h
#import <UIKit/UIKit.h>@interface ViewController : UIViewController@end
ViewController.m
#import "ViewController.h"@interface ViewController ()<NSURLConnectionDataDelegate>- (IBAction)synGET:(id)sender;- (IBAction)synPOST:(id)sender;- (IBAction)asynGET:(id)sender;- (IBAction)asynPOST:(id)sender;- (IBAction)asynGETBlock:(id)sender;@property(nonatomic, retain)NSMutableData *data;@property(nonatomic, retain)UIImageView *imageView;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(110, 400, 150, 150)]; self.imageView.backgroundColor = [UIColor cyanColor]; [self.view addSubview:self.imageView]; [_imageView release];// NSUserDefaults}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}- (IBAction)synGET:(id)sender { // 同步GET请求 NSString *strURL = @"http://api.map.baidu.com/place/v2/search?query=银行®ion=大连&output=json&ak=6E823f587c95f0148c19993539b99295"; // 因为网址里面不允许有汉字, 只能有26个字母的大小写, 和一些特定的符号, 不如&, %, / 等吗所以有中文的网址要先把中文编程相对应的数字编码 // 时间戳 NSString *strURLEncode = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; // 1. 创建一个URL NSURL *url = [NSURL URLWithString:strURLEncode]; // 2. 发送一个请求 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; // 3. 建立一个连接// NSURLSession 网络请求 自己看 // 参数1: 把创建好的请求发送 // 参数2: 返回的响应信息 // 参数3: 错误信息 NSURLResponse *response = nil; NSError *error = nil; NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; // 把data进行json解析 NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; NSLog(@"**同步get**%@", dic);// NSLog(@"%@", response);}- (IBAction)synPOST:(id)sender { // post请求需要在请求的过程里, 添加一个body, 添加之后才可以获取数据 NSString *urlStr = @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"; // 1. 创建一个URL NSURL *url = [NSURL URLWithString:urlStr]; // 2. 创建一个请求 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; // 指定请求的方式, 默认是get请求 [request setHTTPMethod:@"POST"]; // body的字符串 NSString *bodyStr = @"date=20131129&startRecord=1&len=30&udid=1234567890&terminalType=Iphone&cid=213"; // body的字符串变成NSData NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding]; // 把bodyData放到request中 [request setHTTPBody:bodyData]; // 3. 建立一个连接 NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; // json解析 NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; NSLog(@"**同步post**%@", dic);// NSArray *firstArr = dic[@"news"];// for (NSDictionary *tempDic in firstArr) {// NSLog(@"!~!%@", tempDic[@"summary"]);// }}- (IBAction)asynGET:(id)sender { NSString *strURL = @"http://img4.duitang.com/uploads/item/201207/28/20120728105310_jvAjW.thumb.600_0.jpeg"; // 1. 创建URL NSURL *url = [NSURL URLWithString:strURL]; // 2. 创建一个请求 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; // 连接 // 这个方法可以在2.0到9.0之间的版本使用, 9.0以上的版本不能使用这个方法, 注意!!! [NSURLConnection connectionWithRequest:request delegate:self];}- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { // 这个方法会返回响应信息 // 初始化装数据的容器 self.data = [NSMutableData data];}- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { // 这个方法会接收返回的数据 // append是累加的意思 [self.data appendData:data];}- (void)connectionDidFinishLoading:(NSURLConnection *)connection { self.imageView.image = [UIImage imageWithData:self.data];}- (IBAction)asynPOST:(id)sender { // 异步post请求数据 NSString *strURL = @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"; // 1. 创建URL NSURL *url = [NSURL URLWithString:strURL]; // 2. 创建一个请求 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; // 设置请求的方式 [request setHTTPMethod:@"POST"]; // body的字符串 NSString *bodyStr = @"date=20131129&startRecord=1&len=30&udid=1234567890&terminalType=Iphone&cid=213"; // body转换成NSData NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding]; // 把bodyData添加到请求中 [request setHTTPBody:bodyData]; // 建立连接 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { // data就是请求下来的数据 // block里进行数据的解析 NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; NSLog(@"**异步post**%@", dic); }];}- (IBAction)asynGETBlock:(id)sender { NSString *strURL = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php"; NSURL *url = [NSURL URLWithString:strURL]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; NSLog(@"**异步getBlock**%@", dic); }];}@end#pragma mark 总结: 常用的请求方式有两种, 一个是GET, 一个是POST, 它俩本质上没有任何区别, 只是POST在请求的时候需要添加一个body, 同步和异步: 都是用异步的方式进行加载, 加载过程中还可以操作其他的功能, 不会出现卡死的情况, 从同步演化出异步, 请求分为三步: 1. 创建URL, 2. 创建请求request, 3. 建立连接, 完成数据请求, iOS9.0之后, NSURLConnection用的越来越局限, NSURLSession未来更重要
0 0
- GET请求 Post请求
- get请求 post请求
- Get请求 Post请求
- 16.POST、GET请求
- iOS get post请求
- http post get 请求
- IOS Get、Post请求
- get post请求
- ios get post请求
- IOS Get、Post请求
- HTTP GET/POST 请求
- GET/POST请求
- WebService get post请求
- http post get请求
- Android POST GET请求
- POST&&&GET请求比较
- java get post 请求
- afinal post get 请求
- Centos 6.4 python 2.6 升级到 2.7
- Linux GCC常用命令
- constraintsWithVisualFormat:参数为NSString型,指定Contsraint的属性,是垂直方向的限定还是水平方向的限定,参数定义一般如下: V:|-(>=XXX) :表
- Android 引用library project
- LUA中的and与or
- get-post请求
- 关于Android的gradle的中文编译错误解决办法
- 基础篇(二) Activity启动模式(launchMode)
- 获得当前时间 及其转换 localtime函数
- typedef struct 小结
- 好想睡个整夜觉
- oracle备份恢复之rman恢复到异机
- java--通过socket和多线程进行多个客服端与服务器的简单通讯--基于tcp
- bash: ifconfig: command not found 解决办法