当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 两个获取http页面的c#函数

ASP.NET
asp.net DiscuzNT登录,退出的代码
ASP .NET中执行控件(如ImageButton、LinkButton等)命令不刷新页面
Microsoft SQL Server 2005 Express 远程访问设置详述,100%成功篇
使用.NET命令行编译器编译项目(如ASP.NET、C#等)
asp.net 去除viewstate
asp.net repeater实现批量删除
asp.net安全、实用、简单的大容量存储过程分页
asp.net 获取图片高度和宽度实例代码
在GridView中LinkButton的属性的应用(如何不用选中就删除这一行)
关于.net(C#)中的跨进程访问的问题
asp.net request.PathInfo实现的url重写
asp.net SqlDataReader绑定Repeater
ASP.NET 动态写入服务器端控件
Discuz .net版本中的短消息系统
asp.net动态加载用户控件,关于后台添加、修改的思考
.net Cookies安全性实践分析
配置Visual Studio 以调试.net framework源代码
asp.net 大文件上传 之 改版了的SlickUpload.HttpUploadModule(Krystalware.SlickUpload.dll)
asp.net Web站点风格切换的实现
asp.net 用户控件中图片及样式问题

ASP.NET 中的 两个获取http页面的c#函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 48 ::
收藏到网摘: n/a

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


那个扫描漏洞的应用程序包括源代码我将整理后放到我的站点,如果你需要可以来下载。