使用代理服务器

用HttpWebRequest   
    


    
  System.Uri   url=new   Uri(@"http://www.XXX.XXX/");  
  System.Net.HttpWebRequest   request;  
  System.Net.HttpWebResponse   response;  
  request=(System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);  
  request.Method="GET";  
  WebProxy   proxy=new   WebProxy(代理的IP,代理的端口);  
  proxy=(WebProxy)request.Proxy;  
  response=(System.Net.HttpWebResponse)request.GetResponse();  
  StreamReader   read=new   StreamReader(response.GetResponseStream(),System.Text.Encoding.UTF8);  
  String   html=read.ReadToEnd();  
   
  这样html变量就是抓回来的HTML了,如果代理还要求帐号和密码,那在proxy里设

以下的没仔细看  用Sockets类

you   should   be   using   HttpWebRequest/HttpWebResponse/WebProxy   class   in   System.Net,   it   is   easier,   but   if   you   insist,   try   to   grab   the   ProxySocket:  
   
  http://www.mentalis.org/soft/class.qpx?id=9  
   
  otherwise,   you   have   to   compose   the   HTTP   message   yourself,   for   example   (adapted   from   http://www.c-sharpcorner.com/Network/NetworkProgramPart2RVS.asp)  
   
  using   System;  
  using   System.Net;  
  using   System.Net.Sockets;  
  using   System.Text;      
   
  class   MyClient  
  {  
                          public   static   void   Main()  
                          {  
  string   proxyserver   =   "YourProxyServerAddress";  
  int   port   =   8080;  
   
  string   webserver   =   "www.google.com";  
                                                  IPHostEntry   IPHost   =   Dns.Resolve(proxyserver);  
                                                  Console.WriteLine(IPHost.HostName);  
                                                  string   []aliases   =   IPHost.Aliases;      
   
                                                  IPAddress[]   addr   =   IPHost.AddressList;  
                                                  Console.WriteLine(addr[0]);  
                                                  EndPoint   ep   =   new   IPEndPoint(addr[0],port);      
   
  Socket   sock   =   new   Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);    
   
                                                  sock.Connect(ep);  
                                                  if(sock.Connected)  
                                                                          Console.WriteLine("OK");  
                                                  Encoding   ASCII   =   Encoding.ASCII;  
                                                  string   Get   =   String.Format("GET   http://{0}/   HTTP/1.1/r/nHost:   {0}/r/nConnection:   Close/r/nProxy-Authorization:   true/r/n/r/n",   webserver);  
                                                  Byte[]   ByteGet   =   ASCII.GetBytes(Get);  
                                                  Byte[]   RecvBytes   =   new   Byte[256];  
                                                  sock.Send(ByteGet,   ByteGet.Length,   0);  
                                                  Int32   bytes   =   sock.Receive(RecvBytes,   RecvBytes.Length,   0);  
                                                  Console.WriteLine(bytes);  
                                                  String   strRetPage   =   null;  
                                                  strRetPage   =   strRetPage   +   ASCII.GetString(RecvBytes,   0,   bytes);  
                                                  while   (bytes   >   0)  
                                                  {  
                                                                          bytes   =   sock.Receive(RecvBytes,   RecvBytes.Length,   0);  
                                                                          strRetPage   =   strRetPage   +   ASCII.GetString(RecvBytes,   0,   bytes);  
                                                                          Console.WriteLine(strRetPage   );  
                                                  }  
                                                  sock.Shutdown(SocketShutdown.Both);  
                                                  sock.Close();  
                          }  
  }     
    
 

原文地址:https://www.cnblogs.com/liehuo123/p/5562260.html