手机
当前位置:查字典教程网 >编程开发 >编程语言综合 >C#使用代理IP使用方法
C#使用代理IP使用方法
摘要:简要介绍一:WebProxy:即HTTP代理设置。官方解释:WebProxy类包含WebRequest实例用以确定是否使用Web代理发送请求...

简要介绍一:WebProxy :即HTTP 代理设置。

官方解释:WebProxy 类包含 WebRequest 实例用以确定是否使用 Web 代理发送请求的代理设置。 可以在计算机和应用程序配置文件中指定全局 Web 代理设置,并且应用程序可用 WebProxy 类的实例自定义 Web 代理的用途。

个人理解:即将代理IP、Port进行封装,并设置代理IP的用户名及密码,通过该用户名和密码登陆登陆代理主机并进行相关访问。

C#使用代理IP使用方法1

简要介绍二: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(

httpRequest.Method = GET

httpRequest.Credentials = CredentialCache.DefaultCredentials;

// 设置代理属性WebProxy -------------------------------------------------

WebProxy proxy = new WebProxy();

proxy.Address = new Uri(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(58.22.62.163:888/

proxy.Credentials = new NetworkCredential(juese, 1029

wc.Proxy = proxy;

Stream PageHtml = wc.OpenRead(

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 = pileAssemblyFromDom(cplist, ccu);

// 生成代理实例,并调用方法

System.Reflection.Assembly assembly = piledAssembly;

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传入,并异步调用易行接口

【C#使用代理IP使用方法】相关文章:

Ruby常用文件操作方法

Lua中..和#运算符的使用方法

详解Lua中的if语句的使用方法

Java线程调度之线程休眠用法分析

Ruby中的钩子方法详解

C#关闭指定名字进程的方法

C#中调用SAPI实现语音识别的2种方法

C# WebClient类用法实例

python删除过期文件的方法

JAVA中IP和整数相互转化的方法

精品推荐
分类导航