簡要介紹一:WebProxy :即HTTP 代理設定。
官方解釋:WebProxy 類包含 WebRequest 執行個體用以確定是否使用 Web Proxy發送請求的代理設定。 可以在電腦和應用程式設定檔中指定全域 Web Proxy設定,並且應用程式可用 WebProxy 類的執行個體自訂 Web Proxy的用途。
個人理解:即將代理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傳入,並非同步呼叫易行介面