//html
// ViewController.mapi
// UI-NO.18瀏覽器
//安全
// Created by Bruce on 15/8/11.服務器
// Copyright (c) 2015年 Bruce. All rights reserved.網絡
//異步
/*ide
http協議:post
超文本傳輸協議(HTTP,HyperText Transfer Protocol)是互聯網上應用最爲普遍的一種網絡協議。全部的WWW文件都必須遵照這個標準。url
http是用於www(萬維網)瀏覽傳輸數據的一個協議
訪問的是遠程的網絡資源,格式是http://
http://服務器地址 資源的位置
// http://www.baidu.com/user/chginfo
WWW是環球信息網的縮寫,(亦做「Web」、「WWW」、「'W3'」,英文全稱爲「World Wide Web」),中文名字爲「萬維網」,"環球網"等,常簡稱爲Web。 分爲Web客戶端和Web服務器程序。 WWW可讓Web客戶端(經常使用瀏覽器)訪問瀏覽Web服務器上的頁面。 是一個由許多互相連接的超文本組成的系統,經過互聯網訪問。在這個系統中,每一個有用的事物,稱爲同樣「資源」;而且由一個全局「統一資源標識符」(URL)標識;這些資源經過超文本傳輸協議(Hypertext Transfer Protocol)傳送給用戶,然後者經過點擊連接來得到資源。
萬維網聯盟(英語:World Wide Web Consortium,簡稱W3C),又稱W3C理事會。1994年10月在麻省理工學院(MIT)計算機科學實驗室成立。萬維網聯盟的建立者是萬維網的發明者蒂姆·伯納斯-李。
IP協議對應於網絡層,TCP協議對應於傳輸層,而HTTP協議對應於應用層(識別數據內容),
http協議的做用:
(1)規定客戶端和服務器之間的數據傳輸格式
(2)讓客戶端和服務器能有效地進行數據溝通
爲何選擇使用HTTP
(1)簡單快速 由於HTTP協議簡單,因此HTTP服務器的程序規模小,於是通訊速度很快
(2)靈活 HTTP容許傳輸任意類型的數據
(3)HTTP 是非持續鏈接 限制每次鏈接只處理一個請求,服務器對客戶端的請求作出響應後,立刻斷開鏈接,這種方式能夠節省傳輸時間
HTTP的通訊過程
(1)請求:客戶端向服務器索要數據
(2)響應:服務器返回客戶端相應的數據
*****
HTTP的請求方法:get post
get:會把請求的內容 拼接到 連接 地址 裏面(數據請求的時候 默認是get)
www.baidu.com/user/login?username=劉水,psw=123
get特徵:
一、瀏覽器和服務器對URL長度有限制,所以在URL後面附帶的參數是有限制的,一般不能超過1KB
二、會把請求的數據 暴露在接口裏面
post 參數所有放在請求體中
這樣就保證了 數據的安全
沒有具體的長度限制(惟一的限制 就是 服務器的承受能力)
選擇GET和POST的建議
(1)若是要傳遞大量數據,好比文件上傳,只能用POST請求
(2)GET的安全性比POST要差些,若是包含機密\敏感信息,建議用POST
(3)若是僅僅是索取數據(數據查詢),建議使用GET
(4)若是是增長、修改、刪除數據,建議使用POST
URL:Uniform Resource Locator(統一資源定位符)
經過1個URL,能找到互聯網上惟一的1個資源
// 字符串讀取網絡數據
NSURL *url = [NSURL URLWithString:@"http://baike.baidu.com/link?url=vis7S5gBFGNFsbWKyvohMqFDMirD45BqSe23YRkb4481UWxZBMHeIEIpQ3XvZLGlWT11XIzxr4_T7R7dEH7GcK"];
NSString *recvieString = [[NSString alloc]initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",recvieString);
*/
/*
// 讀取網絡圖片
NSURL *url = [NSURL URLWithString:@"http://img.baidu.com/img/baike/logo-baike.png"];
NSData *imageData = [[NSData alloc]initWithContentsOfURL:url];
NSLog(@"%@",imageData);
imageView.image = [UIImage imageWithData:imageData];
*/
// 同步請求
// 加載完網絡數據 纔會執行其餘操做
// 會暫時 暫停用戶響應 不能夠操做手機 當網絡請求結束後 恢復響應
// 異步加載 不會阻塞主線程 不會暫停用戶響應 看界面 下載網絡數據 是不互相影響的
// 同步請求
// 請求類 NSURLRequest
// 鏈接類 NSURLConnection
// 同步請求步驟:
// 一、有連接地址(URL)
// 二、建立請求(NSURLRequest)
// 三、鏈接請求地址 發送請求 返回數據(NSURLConnection)
/*
// 一、有連接地址(URL)
NSURL *url = [NSURL URLWithString:@"http://img.baidu.com/img/baike/logo-baike.png"];
// 二、建立請求(NSURLRequest)
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 三、鏈接請求地址 發送請求 返回數據
NSData *recvieData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
UIImage *image = [UIImage imageWithData:recvieData];
imageView.frame = CGRectMake(100, 100, image.size.width, image.size.height);
imageView.image = image;
*/
/*
// 異步請求
// 一、建立URL地址
// 二、建立請求
// 三、鏈接地址發送異步請求
NSURL *url = [NSURL URLWithString:@"http://m.weather.com.cn/data/101010100.html"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 發送異步請求
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// connectionError 錯誤信息
if (!connectionError) {
// response 服務器迴應的信息
NSLog(@"response%@",response);
// data 服務器返回的數據內容
NSLog(@"data:%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
}
}];
// get方式請求數據的步驟
// 一、拼接url的連接地址字符串(把須要發送的內容 拼接到連接地址裏面)
// 二、初始化URL
// 三、建立一個請求(可變的請求,須要設置請求的方式)
// 四、發送請求 返回請求內容
// 須要發送的請求內容
NSString *sendMessage = @"101010100";
// 拼接請求地址
NSString *urlString = [NSString stringWithFormat:@"http://m.weather.com.cn/data/%@.html",sendMessage];
// 初始化URL
NSURL *url = [NSURL URLWithString:urlString];
// 建立請求 設置請求方式
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5];
// 設置請求的方式種類
request.HTTPMethod = @"GET";
__block NSDictionary *recDic = [NSDictionary dictionary];
// 發送請求
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
// 解析JASN數據
recDic = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
// NSLog(@"%@",recDic);
// 解決 block裏面參數不能外傳的問題(不是block的問題 而是異步加載 在不一樣線程的問題 子線程還未執行完 就已經執行主線程的方法 這時候數據就是空的)
// 等待子線程裏面的任務執行完 在主線程裏執行這個方法
[self performSelectorOnMainThread:@selector(didRecvieData:) withObject:recDic waitUntilDone:YES];
}];
// POST 請求步驟
// 一、準備POST的數據
// 二、初始化URL
// 三、建立請求 設置請求 (設置請求的方式POST 以及 POST的BODY)
// 四、發送請求 返回數據
// 一、準備POST的數據
NSString *bodyString = @"PlatformType=3&serviceId=39&brandId=11";
// 二、初始化URL
NSURL *url = [NSURL URLWithString:@"http://www.weihuok.com/customer2/GetFault"];
// 三、建立請求 設置請求 (設置請求的方式POST 以及 POST的BODY)
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5];
request.HTTPMethod = @"POST";
// HTTPBody 是nsdata類型 須要把 post的數據轉成對應格式
request.HTTPBody = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
// 四、發送請求 返回請求數據
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
}];
*/
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
[self loadData6];
}
//直接讀取網頁中的字符串
- (void)loadData1
{
NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"];
NSString *content = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",content);
}
//直接讀取 網絡圖片
- (void)loadData2
{
NSURL *url = [NSURL URLWithString:@"http://preview.quanjing.com/is_rm001/is0997q92.jpg"];
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.frame];
imageView.image = [UIImage imageWithData:imageData];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:imageView];
}
//同步請求
- (void)loadData3
{
NSURL *url = [NSURL URLWithString:@"http://img.baidu.com/img/baike/logo-baike.png"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSError *error;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
NSLog(@"error:%@",error);
UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.frame];
imageView.image = [UIImage imageWithData:data];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:imageView];
}
//異步請求
- (void)loadData4
{
UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.frame];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:imageView];
NSURL *url = [NSURL URLWithString:@"http://img.baidu.com/img/baike/logo-baike.png"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
imageView.image = [UIImage imageWithData:data];
}];
}
//get
- (void)loadData5
{
NSString *string = @"http://apis.baidu.com/showapi_open_bus/mobile/find?num=13370116152";
NSURL *url = [NSURL URLWithString:[string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *apiKey = @"e7f5ac9e7c42a6c8cb125ee1d7e8779e";
NSLog(@"%@",url);
NSMutableURLRequest *requst = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
[requst addValue:apiKey forHTTPHeaderField:@"apikey"];
requst.HTTPMethod = @"GET";
[NSURLConnection sendAsynchronousRequest:requst queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"==%@",dic);
}];
}
- (void)loadData6
{
// 一、準備POST的數據
NSString *string = @"http://www.weihuok.com/customer2/GetService";
// 二、初始化URL
NSURL *url = [NSURL URLWithString:[string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
// 三、建立請求 設置請求 (設置請求的方式POST 以及 POST的BODY)
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5];
request.HTTPMethod = @"POST";
// HTTPBody 是nsdata類型 須要把 post的數據轉成對應格式
request.HTTPBody = [[NSString stringWithFormat:@"%@",@{@"PlatformType":@"3"}] dataUsingEncoding:NSUTF8StringEncoding];
// 四、發送請求 返回請求數據
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSLog(@"=%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
NSDictionary *content = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@",content);
}];
}
- (void)loadData
{
NSString *apiKey = @" e7f5ac9e7c42a6c8cb125ee1d7e8779e";
NSNumber *idNum = @(-1);
NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"http://apis.baidu.com/myml/c1c/c1c?%@",idNum]];
// 同步請求
// 建立請求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
[request addValue:apiKey forHTTPHeaderField:@"apikey"];
// 鏈接服務器數據
NSData *respondData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
// 把NSData 轉成 字符串類型
NSString *respondString = [[NSString alloc]initWithData:respondData encoding:NSUTF8StringEncoding];
NSLog(@"%@",respondString);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end