首页 > 开发 > 综合 > 正文

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

2024-07-21 02:24:39
字体:
来源:转载
供稿:网友

    埋头苦干一天终于搞定!一个用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 ;
        }


那个扫描漏洞的应用程序包括源代码我将整理后放到我的站点,如果你需要可以来下载。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表