日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > C# >内容正文

C#

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

發(fā)布時間:2024/10/8 C# 98 豆豆
生活随笔 收集整理的這篇文章主要介紹了 两个获取http页面的c#函数 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一個用C#寫的windows應(yīng)用程序,作用嘛,就是對asp程序已知的20種漏洞進(jìn)行掃描,顯示源程序。在這個應(yīng)用程序中用到兩種獲得http頁面的方 法,一種是直接用httpwebrequest類,而另一種是同服務(wù)器通過tcp/ip建立socket連接,直接查詢端口80 , 為此我寫了以下兩個函數(shù),第一個比較簡單,參數(shù)只有一個,就是要求的url , 另外一個比較復(fù)雜,也很通用,不僅可以請求http頁面,還可以和其他端口通信,如端口43的whois,端口25的smtp,端口21的ftp甚至 pop3等等,三個參數(shù)分別是主機(jī)名,請求命令和端口。好了,看程序吧。
??
?? //獲取http頁面函數(shù)
?? 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連接,發(fā)送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 ;
?? }

轉(zhuǎn)載于:https://www.cnblogs.com/top5/archive/2010/02/23/1671686.html

總結(jié)

以上是生活随笔為你收集整理的两个获取http页面的c#函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。