/*git
HTTP 是应用层的网络传输协议,对HTTP请求主要流行的方式有两种 GET请求以及POST请求安全
GET请求和POST请求区别和联系:服务器
1.GET请求,服务器网址以及参数都会出如今咱们网络请求的接口中,也就是说参数会做为请求接口的一部分. 而POST请求在接口中只有服务器网址,而参数会做为请求体交给服务器网络
2.由于GET请求参数会做为接口的一部分出如今接口中,因此信息容易捕获,不安全.而POST请求参数会被封装在请求体中,做为二进制数据进行传输,不易捕获,安全性高app
3.由于接口有字节限制,虽说从理论上GET请求和POST请求均可以进行请求数据和上传数据,GET请求参数会占用字节,因此只能进行小数据上传.而对于POST请求参数在请求体二进制数据中,理论上是无限的.因此,咱们通常使用GET请求获取数据,POST请求上传数据.异步
*/网络传输协议
//同步GET编码
- (IBAction)synGET:(id)sender {url
//1.建立NSURL 对象代理
//(1)获取urlString
NSString *urlString = [NSString stringWithFormat:@"%@",kPicURL];
//(2)从新编码
NSString *newStr = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//(3)获取网址
NSURL *url = [NSURL URLWithString:newStr];
//2.建立NSURLRequest 对象
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//3.请求数据
//
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"==%@",response);
NSLog(@"++%@",error);
NSLog(@"--%@",data);
self.imageView.image = [UIImage imageWithData:data];
//解析
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
NSLog(@"%@",dic);
}
//同步POST
- (IBAction)synPOST:(id)sender {
//1.建立NSURL对象
NSString *urlString = [NSString stringWithFormat:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"];
NSURL *url = [NSURL URLWithString:urlString];
//2.建立可变请求对象
NSMutableURLRequest *muRequest = [NSMutableURLRequest requestWithURL:url];
//3.设置请求体 和 请求方式
//(1)建立请求参数
NSString *parameterStr = [NSString stringWithFormat:@"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"];
//(2)将请求参数封装成NSDate对象
NSData *paraData = [parameterStr dataUsingEncoding:NSUTF8StringEncoding];
//设置请求体 请求方式
[muRequest setHTTPBody:paraData];
[muRequest setHTTPMethod:@"POST"];
//4.请求数据
NSData *data = [NSURLConnection sendSynchronousRequest:muRequest returningResponse:nil error:nil];
//...数据处理
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"%@",dic);
}
//异步GET
- (IBAction)asynGET:(id)sender {
//1.建立NSURL对象
//(1)建立urlString
NSString *urlString = [NSString stringWithFormat:kPicURL];
//(2)从新编码
NSString *newStr = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//(3)url
NSURL *url = [NSURL URLWithString:newStr];
//2.建立request对象
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//3.请求数据
//a 使用代理方法回调进行异步网络请求
// [NSURLConnection connectionWithRequest:request delegate:self];
//b.使用block进行异步网络请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//当数据获取成功时执行
self.imageView.image = [UIImage imageWithData:data];
}];
}
//异步POST
- (IBAction)asynPOST:(id)sender {
//1.建立NSURL对象
NSString *urlString = [NSString stringWithFormat:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"];
NSURL *url = [NSURL URLWithString:urlString];
//2.建立可变的请求对象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//3.设置请求体 和 请求方式
//(1)获取参数字符串
NSString *paraString = [NSString stringWithFormat:@"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"];
//(2)转化为NSData
NSData *data = [paraString dataUsingEncoding:NSUTF8StringEncoding];
request.HTTPBody = data;
request.HTTPMethod = @"POST";
//4.请求
//a 代理方法回调
[NSURLConnection connectionWithRequest:request delegate:self];
//b block
// [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
// NSLog(@"%@",dic);
// }];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - connectionDelegate
//链接到服务器
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
//当链接到服务器时 建立对象
self.data = [NSMutableData data];
NSLog(@"创建链接");
}
//获取数据时
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
//拼接数据
[self.data appendData:data];
NSLog(@"获取数据");
}
//完成请求
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"请求完成");
//数据请求完成 能够进行数据的操做
self.imageView.image = [UIImage imageWithData:_data];
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:_data options:0 error:nil];
NSLog(@"%@",dic);
}
//请求失败
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"请求失败");
}
- (void)dealloc {
[_imageView release];
[super dealloc];
}