您现在的位置: 万盛学电脑网 >> 程序编程 >> 网络编程 >> 编程语言综合 >> 正文

C#使用代理IP使用方法

作者:佚名    责任编辑:admin    更新时间:2022-06-22

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

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

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

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