簡要介紹一:WebProxy :即HTTP 代理設置。
官方解釋:WebProxy 類包含 WebRequest 實例用以確定是否使用 Web 代理發送請求的代理設置。 可以在計算機和應用程序配置文件中指定全局 Web 代理設置,並且應用程序可用 WebProxy 類的實例自定義 Web 代理的用途。
個人理解:即將代理IP、Port進行封裝,並設置代理IP的用戶名及密碼,通過該用戶名和密碼登陸登陸代理主機並進行相關訪問。
簡要介紹二:HttpWebClientProtocol:所有使用 HTTP 傳輸協議的 xm l Web services 客戶端代理的基類。
在調用易行接口時,會動態編譯源碼,將編譯後創建的實例強制轉換成HttpWebClientProtocol類型,並在HttpWebClientProtocol中附上proxy類型,即可使用代理IP進行訪問。
簡要介紹三:在HttpWebRequest、WebClien、HttpWebClientProtocol都可以使用代理IP。
一: HttpWebRequest:已Http形式抓取網頁,僅需在發起http前給request加上proxy屬性即可,如下面使用代理IP抓取百度首頁:
HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create("http://www.baidu.com");
httpRequest.Method = "GET";
httpRequest.Credentials = CredentialCache.DefaultCredentials;
// 設置代理屬性WebProxy -------------------------------------------------
WebProxy proxy = new WebProxy();
proxy.Address = new Uri("http://58.22.62.163:888/");
proxy.Credentials = new NetworkCredential("juese", "1029");
// 在發起HTTP請求前將proxy賦值給HttpWebRequest的Proxy 屬性
httpRequest.Proxy = proxy;
//-------------------------------------------------
HttpWebResponse res = (HttpWebResponse)httpRequest.GetResponse();
StreamReader reader = new StreamReader(res.GetResponseStream(), System.Text.Encoding.UTF8);
string content = reader.ReadToEnd();
reader.Close();
二:WebClien:與上面類似,
WebClient wc = new WebClient();
WebProxy proxy = new WebProxy();
proxy.Address = new Uri("http://58.22.62.163:888/");
proxy.Credentials = new NetworkCredential("juese", "1029");
wc.Proxy = proxy;
Stream PageHtml = wc.OpenRead("http://www.baidu.com");
StreamReader reader = new StreamReader(PageHtml, System.Text.Encoding.UTF8);
string content = reader.ReadToEnd();
return content;
三:HttpWebClientProtocol:針對webService的代理IP使用(詳情可參加TTS交互服務的WebServiceHelper.cs):
// 獲取WSDL
WebClient wc = new WebClient();
stream = wc.OpenRead(url);
ServiceDesc ription sd = ServiceDesc ription.Read(stream);
ServiceDesc riptionImporter sdi = new ServiceDesc riptionImporter();
sdi.AddServiceDesc ription(sd, string.Empty, string.Empty);
CodeNamespace cn = new CodeNamespace(@namespace);
// 生成客戶端代理類代碼
CodeCompileUnit ccu = new CodeCompileUnit();
ccu.Namespaces.Add(cn);
sdi.Import(cn, ccu);
CSharpCodeProvider icc = new CSharpCodeProvider();
// 設定編譯參數
CompilerParameters cplist = new CompilerParameters();
cplist.GenerateExecutable = false;
cplist.GenerateInMemory = true;
cplist.ReferencedAssemblies.Add("System.dll");
cplist.ReferencedAssemblies.Add("System.xm l.dll");
cplist.ReferencedAssemblies.Add("System.Web.Services.dll");
cplist.ReferencedAssemblies.Add("System.Data.dll");
////此處不停編譯,會造成內存洩露
// 編譯代理類
cr = icc.CompileAssemblyFromDom(cplist, ccu);
// 生成代理實例,並調用方法
System.Reflection.Assembly assembly = cr.CompiledAssembly;
Type t = assembly.GetType(@namespace + "." + classname, true, true);
ob ject obj = Activator.CreateInstance(t);
if (ConfigurationManager.AppSettings["UseYeexingProxy"] == "true")
{
ICredentials cred;
WebProxy p = null;
var prox = obj as HttpWebClientProtocol;
string proxyAddressAndPort = ConfigurationManager.AppSettings["ProxyIP"];
string proxyUserName = ConfigurationManager.AppSettings["ProxyName"];
string proxyPassword = ConfigurationManager.AppSettings["ProxyPwd"];
cred = new NetworkCredential(proxyUserName, proxyPassword);
p = new WebProxy(proxyAddressAndPort, true, null, cred);
prox.Proxy = p;
System.Reflection.MethodInfo mi = t.GetMethod(methodname);
return mi.Invoke(prox, args);
}
else
{
System.Reflection.MethodInfo mi = t.GetMethod(methodname);
return mi.Invoke(obj, args);
}
在網上查看相關文檔,很容易將Proxy屬性加在WebClient上,然而此處卻直接將Proxy傳入,並異步調用易行接口