iOS開發(fā)ASIHTTPRequest使用代理連接
ASIHTTPRequest檢測(cè)系統(tǒng)的proxy設(shè)置并自動(dòng)將proxy用于request。從1.0.6版本開始,它還支持PAC文件和要求授權(quán)的proxy。
默認(rèn)情況下,ASIHTTPRequest將嘗試自動(dòng)檢測(cè)proxy設(shè)置。當(dāng)然,你可以看自己手動(dòng)設(shè)置:
- // 手動(dòng)設(shè)置代理服務(wù)器
- NSURL *url = [NSURL URLWithString:@"http://www.dreamingwish.com"];
- ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
- [request setProxyHost:@"192.168.0.1"];
- [request setProxyPort:3128];
- // 另一種方法, 使用代理配置腳本文件
- // (***使用本地pac文件)
- [request setPACurl:[NSURL URLWithString:@"path/to/test.pac"]];
要求授權(quán)的proxy
在Mac OS上,ASIHTTPRequest可以自動(dòng)檢測(cè)到要求授權(quán)的proxy的憑據(jù)(前提是在系統(tǒng)設(shè)定中設(shè)置好)。在iOS上,ASIHTTPRequest則無法自動(dòng)檢測(cè)出授權(quán)憑據(jù),所以你要么手動(dòng)使用delegate來向你的controller或者用戶索取合適的憑據(jù),要么讓ASIAuthenticationDialog向用戶索取憑據(jù)。一旦獲得了一個(gè)有效的proxy憑據(jù),那么該憑據(jù)將被存儲(chǔ)到keychian中(前提是啟用useKeychainPersistence )并自動(dòng)重用。
手動(dòng)為proxy指定憑據(jù)
- NSURL *url = [NSURL URLWithString:@"http://www.dreamingwish.com"];
- ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
- [request setProxyHost:@"192.168.0.1"];
- [request setProxyPort:3128];
- //為要求授權(quán)的proxy設(shè)置username 和password
- [request setProxyUsername:@"bencopsey"];
- [request setProxyPassword:@"password"];
- // 對(duì)于NTLM proxy,還要設(shè)置 domain (NTLM proxy功能是未經(jīng)測(cè)試的)
- [request setProxyDomain:@"la.la.land"];
使用delegate來提供proxy憑據(jù)
這個(gè)特性的工作原理和“使用delegate提供HTTP授權(quán)”一樣,只有一點(diǎn)不同:你的delegate要響應(yīng)proxyAuthenticationNeededForRequest:函數(shù)。
使用內(nèi)建的授權(quán)對(duì)話框(僅適用于iOS)
這個(gè)特性歸功于1.0.8版本的新類ASIAuthenticationDialog 。用來向用戶索取憑據(jù)來授權(quán)webserver或者proxy。
如果你的delegate不響應(yīng)proxyAuthenticationNeededForRequest:函數(shù),那么默認(rèn)情況下,ASIHTTPRequest將會(huì)顯示一個(gè)對(duì)客戶來提示用戶輸入授權(quán)憑據(jù)。使用ASIHTTPRequest,開發(fā)者不再需要寫額外的代碼來顯示授權(quán)對(duì)話框,因?yàn)槟J(rèn)情況下,ASIHTTPRequest就會(huì)顯示它。
使用同步request時(shí)proxy授權(quán)對(duì)話框不會(huì)顯示出來。
如果你不限使用proxy授權(quán)對(duì)話框,那么你要么實(shí)現(xiàn)proxyAuthenticationNeededForRequest:,要么設(shè)置shouldPresentProxyAuthenticationDialog 為false(此時(shí)你的程序?qū)o法連接到proxy)。如果你要改變對(duì)話框的樣式,你得繼承ASIHTTPRequest類,重寫showProxyAuthenticationDialog 來顯示你自己的對(duì)話框或者ASIAuthenticationDialog 子類.