两个获取http页面的c#函数 -尊龙游戏旗舰厅官网
尊龙游戏旗舰厅官网
收集整理的这篇文章主要介绍了
两个获取http页面的c#函数
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
一个用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 ;
}
//获取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 ;
}
转载于:https://www.cnblogs.com/top5/archive/2010/02/23/1671686.html
总结
以上是尊龙游戏旗舰厅官网为你收集整理的两个获取http页面的c#函数的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: java版世界时钟示例
- 下一篇: