網(wǎng)絡(luò)基礎(chǔ)編程3
http長用的方法
GET
- GET 的本質(zhì)是"得"
- 從服務(wù)器拿數(shù)據(jù),效率更高
- 從數(shù)學(xué)的角度來講,GET 的結(jié)果是"冪等"的
- GET請求能夠被緩存
- 在 HTTP 協(xié)議定義中,沒有對 GET 請求的數(shù)據(jù)大小限制,不過因為瀏覽器不同,一般限制在 2~8K 之間
- 所有的參數(shù)包裝在URL中,并且服務(wù)器的訪問日志會記錄,不要傳遞敏感信息
- 參數(shù)格式
- 在資源路徑末尾添加
?表示追加參數(shù) - 每一個變量及值按照
變量名=變量值方式設(shè)定,不能包含空格或者中文 - 多個參數(shù)使用&連接
-
URL 字符串中如果包含中文,需要添加百分號轉(zhuǎn)義
- 在資源路徑末尾添加
POST
- POST 的本質(zhì)是"給"
- 向服務(wù)器發(fā)送數(shù)據(jù),也可以獲得服務(wù)器處理之后的結(jié)果,效率不如 GET
- POST請求不能被緩存
- POST提交數(shù)據(jù)比較大,大小靠服務(wù)器的設(shè)定值限制,PHP通常限定 2M
- URL中,只有資源路徑,但不包含參數(shù),服務(wù)器日志不會記錄參數(shù),相對更安全
- 參數(shù)被包裝成二進制的數(shù)據(jù)體,格式與 GET 基本一致,只是不包含
? - 所有設(shè)計用戶隱私的數(shù)據(jù)(密碼,銀行卡號)一定記住使用
POST方式傳遞
GET 緩存
GET 方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSURL *url = [NSURL URLWithString:@"http://localhost/itcast/images/head1.png"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:1 timeoutInterval:10.0];
if (self.etag.length > 0) {
NSLog(@"%@", self.etag);
[request setValue:self.etag forHTTPHeaderField:@"If-None-Match"];
}
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
NSLog(@"%@", httpResponse);
// 判斷服務(wù)器返回的狀態(tài)碼,是否是 304
if (httpResponse.statusCode == 304) {
NSLog(@"加載緩存數(shù)據(jù)");
NSCachedURLResponse *cachedResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:request];
self.iconView.image = [UIImage imageWithData:cachedResponse.data];
return;
}
self.etag = httpResponse.allHeaderFields[@"Etag"];
self.iconView.image = [UIImage imageWithData:data];
}];
}
Request 緩存請求頭
-
If-None-Match- 與響應(yīng)頭的 Etag 相對應(yīng),可以判斷本地緩存數(shù)據(jù)是否發(fā)生變化
設(shè)置緩存
在appdelegate里邊設(shè)置網(wǎng)絡(luò)緩存的大小
NSURLCache *urlCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024 diskCapacity:20 * 1024 * 1024 diskPath:nil];
[NSURLCache setSharedURLCache:urlCache];
- iOS 5.0開始,支持磁盤緩存,但僅支持 HTTP
- iOS 6.0開始,支持 HTTPS 緩存
GET & POST 登錄
基本代碼
GET 登錄
// MARK: - GET 登錄
- (void)getLogin {
NSString *username = @"zhangsan";
NSString *pwd = @"zhang";
NSString *urlString = [NSString stringWithFormat:@"http://localhost/login.php?username=%@&password=%@", username, pwd];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:2 timeoutInterval:10.0];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
id result = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
NSLog(@"%@ - %@", response, result);
}];
}
POST 登錄
// MARK: POST 登錄
- (void)postLogin {
NSString *username = @"zhangsan";
NSString *pwd = @"zhang";
NSURL *url = [NSURL URLWithString:@"http://localhost/login.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:2 timeoutInterval:10.0];
request.HTTPMethod = @"POST";
NSString *bodyString = [NSString stringWithFormat:@"username=%@&password=%@", username, pwd];
request.HTTPBody = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
id result = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
NSLog(@"%@ - %@", response, result);
}];
}
GET & POST 對比
URL
-
GET - 真正的變化都在 URL 中
- URL 格式
- login.php 負責登錄的腳本(提示,服務(wù)器腳本可以有很多種,php是上課使用的一種)
- 如果接參數(shù),使用
? - 參數(shù)格式,值對:
參數(shù)名=值 - 多個參數(shù),使用
&連接 - 如果 URL 字符串中有中文/空格等特殊字符,需要添加百分號轉(zhuǎn)義
- URL 格式
-
POST
- URL 中不包含任何參數(shù),直接指定腳本路徑即可
Request
-
GET - 是網(wǎng)絡(luò)訪問使用頻率最高的方法,從服務(wù)器獲取數(shù)據(jù),
URLRequest的默認方法就是GET- 不需要做任何改動
-
POST
- 需要指定 HTTP 的訪問方法:POST
- 所有的數(shù)據(jù)參數(shù)都在數(shù)據(jù)體中指定,數(shù)據(jù)內(nèi)容可以從
Firebug中粘貼 - 數(shù)據(jù)格式和
GET方法的參數(shù)定義非常類似,沒有?
Connection
- 將
請求發(fā)送給服務(wù)器 - 返回服務(wù)器的二進制數(shù)據(jù)實體
- 是網(wǎng)絡(luò)訪問中,最單純的方法
- 無論
GET還是POST方法,Connection 沒有變化
GET & POST 補充
百分號轉(zhuǎn)義
-
GET方法的 URL 中如果包含中文或者特殊字符,需要添加百分號轉(zhuǎn)義 -
POST方法不需要添加百分號轉(zhuǎn)義
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
緩存(知道就好)
- GET 能夠緩存
- POST 不能緩存
緩存
- GET 能夠緩存
- POST 不能緩存
總結(jié):通過本篇博客我們學(xué)習(xí)到了get,post登陸,和get,post緩存的設(shè)置,和get我們從網(wǎng)絡(luò)上加載數(shù)據(jù)實時更新,因為默認是從網(wǎng)絡(luò)上加載數(shù)據(jù),我們需要設(shè)置一個If-None-Match的值,post沒有本地緩存所以我們不需要設(shè)置,也學(xué)習(xí)到了get,post請求的一些區(qū)別,get的請求速度快,但是不安全,post的把所有的數(shù)據(jù)都封裝在請求頭里邊所以比較安全