HTTP和HTTPS协议
- URL全称是Uniform ResourceLocator(统一字典定位符)通过一个URL,能够找到互联网上唯一的11个资源
- URL就是资源的地址,位置,互联网上的每个资源都有一个唯一URL
- URL的基本格式 = 协议://主机地址/路径()如http://www.lanou3g.com/szzr/
- 协议:不同的协议代表着不同国的资源查找方式,资源传输方式
- 主机地址:存放资源的主机的IP地址(域名)
- 路径:资源在主机中的位置
- HTTP协议:Hyper Text Transfer Protocol(超文本传输协议)是用于从万维网(www)服务器传送超文本到本地浏览器的传输协议,HTTP是一个应用层协议,由请求和响应构成,是一个标准的客户端服务器模型,客户端向服务器发送一个请求报文,服务器以一个状态作为响应.
- C/S模式:Client和Server常常分别处在相距很远的两台计算机上,Client程序任务是将用户的要求提交给Server程序,再将server程序返回的结果以特定的形式显示给用户;Server程序的任务是接收客户程序提出的服务请求,进行相应的处理,再将结果返回给客户程序.
- HTTPS(Secure Hyper Text Transfer Protocol)安全超文本传输协议,它是一个安全通道.基于HTTP开发,用于在客户计算机和服务器之间的交换信息,它使用安全套接字层(SSL)进行信息交换,简单来说它是HTTP的安全版.HTTPS协议使用SSL在发送和接受方通过交换共和的密匙来实现,因此,所传送的数据不容易被网络黑客截获和解密.
- SSL是Netscape公司所提出安全保密协议,运行在TCP/IP层之上,应用层之下,为应用层提供加密数据通道
-
HTTP和HTTPS的异同:
- https协议需要到cd申请证书,一般免费证书很少,需要交费.
- http是超文本传输协议,信息是明文传输,https则是具有安全性的ssl加密传输协议
- http和https使用的是完全不同的链接方式,用的端口也不一样,前者是80,后者是443
- http的链接很简单,是无状态的
- https是协议是由SSL+HTTP协议构建的可进行加密传输,身份认证的网络协议,要比http安全.
HTTP协议的常见的请求方式:GET和POST
- 相同点:都能给服务器传输数据.
-
不同点:
-
给服务器传输数据的方式不同:
-
传输数据的大小:
- GET:网址字符串最多255.
- 使用NSData,容量超过1G.
-
安全性:
- GET:所有传输给服务的数据,显示的网址里,类似于密码的明文输入,直接可见.
- POST:数据被转成NSData(二进制数据),类似于密码的密文输入,无法直接读取.
HTTP协议请求如何实现
-
NSURL
-
连接方式:
- 同步连接:程序容易出现卡死现象
-
异步连接:等待数据返回
同步请求
GetSynchronization get同步请求
- (void)getAndSynchronization {
NSString *urlString = @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?date=20151031&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
if (data) {
self.getDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
if(self.getDic){
if ([NSJSONSerialization isValidJSONObject:self.getDic]) {
NSData *strData = [NSJSONSerialization dataWithJSONObject:self.getDic options:NSJSONWritingPrettyPrinted error:nil];
if (strData) {
NSString *str = [[NSString alloc] initWithData:strData encoding:NSUTF8StringEncoding];
NSLog(@"%@",str);
}
}
}
} else {
NSLog(@"请求失败");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
post同步请求
- (void)postAndSynchronization {
NSURL *url = [NSURL URLWithString:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"];
NSString *postStr = @"date=20151031&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213";
NSData *postData = [postStr dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
request.HTTPMethod = @"POST";
request.HTTPBody = postData;
NSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
dic = [NSJSONSerialization JSONObjectWithData:received options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@",dic);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
异步请求
异步block请求
- (void)getAndAsychronousBlock {
NSURL *url = [NSURL URLWithString:@"http://mapi.weibo.com/2/remind/unread_count?remind_version=199&networktype=wifi&with_comment_attitude=1&ext_all=0&moduleID=700&c=android&i=ed0d41d&s=f32076c0&ua=Xiaomi-MI%203__weibo__6.4.0__android__android4.4.4&wm=20005_0002&aid=01AtCI_i9CnkwZ8amfinkM-ZJWFOpF0gsEw_rwX43XUPpDgT8.&idc=&v_f=2&from=1064095010&gsid=_2A256CymIDeTxGeNG6FQQ9i_NwjqIHXVWgTpArDV6PUJbrdAKLXjFkWoNYAQ8b7QA8jEG-5JLCblfTetI5w..&lang=zh_CN&skin=default&oldwm=9975_0028&sflag=1&with_settings=1&unread_message=1&with_page_group=1"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@",dic);
NSLog(@"请求到数据了");
}];
NSLog(@"我在block的地下");
}
post 异步协议代理方式
- (void)postAndDelegate {
NSURL *url = [NSURL URLWithString:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"];
NSMutableURLRequest *mutableURLRequest = [NSMutableURLRequest requestWithURL:url];
[mutableURLRequest setHTTPMethod:@"POST"];
NSString *str = @"date=20151031&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213";
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
[mutableURLRequest setHTTPBody:data];
[mutableURLRequest setTimeoutInterval:60];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:mutableURLRequest delegate:self];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
#pragma mark - NSURLConnectionDelegate,NSURLConnectionDataDelegate协议方法
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
self.receiveData = [[NSMutableData alloc] init];
NSLog(@"开始返回响应");
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(@"开始接收数据");
[self.receiveData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"数据已经返回完毕");
NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:self.receiveData options:NSJSONReadingAllowFragments error:nil];
if (dict) {
if ([NSJSONSerialization isValidJSONObject:dict]) {
NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",str);
}
}
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"请求出错----%@",error);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
iOS7之后请求变化
NSURLSession get请求(block)
- (void)sessionAndGet {
NSURL *url = [NSURL URLWithString:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?date=20151031&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"];
NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@",dic);
}];
[dataTask resume];
}
NSURLSession post请求block
- (void)sessionAndPost {
NSURL *url = [NSURL URLWithString:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
request.HTTPBody = [@"date=20151031&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213 " dataUsingEncoding:NSUTF8StringEncoding];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSMutableDictionary *mDict = [NSMutableDictionary dictionary];
mDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@",mDict);
}];
[task resume];
}
#pragma mark - NSURLSessionDataDelegate,,NSURLSessionDelegate协议方法
- (void)sessionAndDelegate {
NSURL *url = [NSURL URLWithString:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
NSURLSessionDataTask *task = [session dataTaskWithRequest:[NSURLRequest requestWithURL:url]];
[task resume];
}
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler {
completionHandler(NSURLSessionResponseAllow);
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
self.recervedData = [NSMutableData dataWithCapacity:40];
}
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
[self.recervedData appendData:data];
}
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
dic = [NSJSONSerialization JSONObjectWithData:self.recervedData options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@",dic);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
下载
- (void)downLoadTask {
NSURL *url = [NSURL URLWithString:@"http://mapi.weibo.com/2/remind/unread_count?remind_version=199&networktype=wifi&with_comment_attitude=1&ext_all=0&moduleID=700&c=android&i=ed0d41d&s=f32076c0&ua=Xiaomi-MI%203__weibo__6.4.0__android__android4.4.4&wm=20005_0002&aid=01AtCI_i9CnkwZ8amfinkM-ZJWFOpF0gsEw_rwX43XUPpDgT8.&idc=&v_f=2&from=1064095010&gsid=_2A256CymIDeTxGeNG6FQQ9i_NwjqIHXVWgTpArDV6PUJbrdAKLXjFkWoNYAQ8b7QA8jEG-5JLCblfTetI5w..&lang=zh_CN&skin=default&oldwm=9975_0028&sflag=1&with_settings=1&unread_message=1&with_page_group=1"];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDownloadTask *downLoadTask = [session downloadTaskWithURL:url completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error) {
NSLog(@"error:%@",error.description);
} else {
NSURL *targetURL = [NSURL fileURLWithPath:@"/Users/xalo/Desktop/herJSON.json"];
NSFileManager *manager = [NSFileManager defaultManager];
[manager copyItemAtURL:location toURL:targetURL error:nil];
NSLog(@"location:%@",location);
}
}];
[downLoadTask resume];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26