• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

两个获取http页面的c#函数

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
一个用C#写的windows应用程序,作用嘛,就是对asp程序已知的20种漏洞进行扫描,显示源程序。在这个应用程序中用到两种获得http页面的方 法,一种是直接用httpwebrequest类,而另一种是同服务器通过tcp/ip建立socket连接,直接查询端口80 , 为此我写了以下两个函数,第一个比较简单,参数只有一个,就是要求的url , 另外一个比较复杂,也很通用,不仅可以请求http页面,还可以和其他端口通信,如端口43的whois,端口25的smtp,端口21的ftp甚至 pop3等等,三个参数分别是主机名,请求命令和端口。好了,看程序吧。
  
   //获取http页面函数
   private string Get_Http(string a_strUrl)
   {
   string strResult ;
   HttpWebRequest myReq = (HttpWebRequest)
   WebRequestFactory.Create(a_strUrl) ;
  
   try
   {
   HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse();
   Stream myStream = HttpWResp.GetResponseStream () ;
  
   StreamReader sr = new StreamReader(myStream , Encoding.Default);
   StringBuilder strBuilder = new StringBuilder();
   while (-1 != sr.Peek())
   {
   strBuilder.Append(sr.ReadLine()+"\r\n");
   }
  
   strResult = strBuilder.ToString();
   }
   catch(Exception exp)
   {
   strResult = "错误:" + exp.Message ;
   }
  
   return strResult ;
  
   }
  
  
   //通过同server建立tcp/ip连接,发送socket命令
   private string Get_Socket_Request(string a_strServer , string a_strRequest , Int32 a_intPort)
   {
   //Set up variables and String to write to the server
   Encoding ASCII = Encoding.Default ;
   string Get = a_strRequest + "Connection: Close\r\n\r\n";
   //string Get =
   Byte[] ByteGet = ASCII.GetBytes(Get);
   Byte[] RecvBytes = new Byte[256];
   String strRetPage = null;
  
   // IPAddress and IPEndPoint represent the endpoint that will
   // receive the request
   IPAddress hostadd = DNS.Resolve(a_strServer.Substring(7 ,a_strServer.Length - 7));
   IPEndPoint EPhost = new IPEndPoint(hostadd, a_intPort);
  
   //Create the Socket for sending data over TCP
   Socket s = new Socket(AddressFamily.AfINet, SocketType.SockStream,
   ProtocolType.ProtTCP );
  
   // Connect to host using IPEndPoint
   if (s.Connect(EPhost) != 0)
   {
   strRetPage = "Unable to connect to host";
   return strRetPage;
   }
  
   // Sent the GET text to the host
   s.Send(ByteGet, ByteGet.Length, 0);
  
   // Receive the page, loop until all bytes are received
   Int32 bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);
   strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);
  
   while (bytes > 0)
   {
   bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);
   strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);
   }
  
   return strRetPage ;
   }

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#实现邮件发送发布时间:2022-07-10
下一篇:
C# 使用GetOleDbSchemaTable 检索架构信息实例发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap