C#中使用Socket获取网页源代码的代码
程序员文章站
2023-11-27 21:18:58
webtoolkit类: 复制代码 代码如下: using system; using system.net.sockets; using system.text; nam...
webtoolkit类:
using system;
using system.net.sockets;
using system.text;
namespace consoleapplication1
{
class webtoolkit
{
/// <summary>
/// url结构
/// </summary>
struct urlinfo
{
public string host;
public int port;
public string file;
public string body;
}
/// <summary>
/// 解析url
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
private static urlinfo parseurl(string url)
{
urlinfo urlinfo = new urlinfo();
string[] strtemp = null;
urlinfo.host = "";
urlinfo.port = 80;
urlinfo.file = "/";
urlinfo.body = "";
int intindex = url.tolower().indexof("http://");
if (intindex != -1)
{
url = url.substring(7);
intindex = url.indexof("/");
if (intindex == -1)
{
urlinfo.host = url;
}
else
{
urlinfo.host = url.substring(0, intindex);
url = url.substring(intindex);
intindex = urlinfo.host.indexof(":");
if (intindex != -1)
{
strtemp = urlinfo.host.split(':');
urlinfo.host = strtemp[0];
int.tryparse(strtemp[1], out urlinfo.port);
}
intindex = url.indexof("?");
if (intindex == -1)
{
urlinfo.file = url;
}
else
{
strtemp = url.split('?');
urlinfo.file = strtemp[0];
urlinfo.body = strtemp[1];
}
}
}
return urlinfo;
}
/// <summary>
/// 发出请求并获取响应
/// </summary>
/// <param name="host"></param>
/// <param name="port"></param>
/// <param name="body"></param>
/// <param name="encode"></param>
/// <returns></returns>
private static string getresponse(string host, int port, string body, encoding encode)
{
string strresult = string.empty;
byte[] btesend = encoding.ascii.getbytes(body);
byte[] btereceive = new byte[1024];
int intlen = 0;
using (socket socket = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp))
{
try
{
socket.connect(host, port);
if (socket.connected)
{
socket.send(btesend, btesend.length, 0);
while ((intlen = socket.receive(btereceive, btereceive.length, 0)) > 0)
{
strresult += encode.getstring(btereceive, 0, intlen);
}
}
socket.close();
}
catch { }
}
return strresult;
}
/// <summary>
/// get请求
/// </summary>
/// <param name="url"></param>
/// <param name="encode"></param>
/// <returns></returns>
public static string get(string url, encoding encode)
{
urlinfo urlinfo = parseurl(url);
string strrequest = string.format("get {0}?{1} http/1.1\r\nhost:{2}:{3}\r\nconnection:close\r\n\r\n", urlinfo.file, urlinfo.body, urlinfo.host, urlinfo.port.tostring());
return getresponse(urlinfo.host, urlinfo.port, strrequest, encode);
}
/// <summary>
/// post请求
/// </summary>
/// <param name="url"></param>
/// <param name="encode"></param>
/// <returns></returns>
public static string post(string url, encoding encode)
{
urlinfo urlinfo = parseurl(url);
string strrequest = string.format("post {0} http/1.1\r\nhost:{1}:{2}\r\ncontent-length:{3}\r\ncontent-type:application/x-www-form-urlencoded\r\nconnection:close\r\n\r\n{4}", urlinfo.file, urlinfo.host, urlinfo.port.tostring(), urlinfo.body.length, urlinfo.body);
return getresponse(urlinfo.host, urlinfo.port, strrequest, encode);
}
}
}
调用示例:
using system;
using system.text;
namespace consoleapplication1
{
//调用示例
class program
{
public static void main(string[] args)
{
console.writeline(webtoolkit.get("//www.jb51.net/t.asp?keyword=vbscript", encoding.default));
console.readkey();
}
}
}
复制代码 代码如下:
using system;
using system.net.sockets;
using system.text;
namespace consoleapplication1
{
class webtoolkit
{
/// <summary>
/// url结构
/// </summary>
struct urlinfo
{
public string host;
public int port;
public string file;
public string body;
}
/// <summary>
/// 解析url
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
private static urlinfo parseurl(string url)
{
urlinfo urlinfo = new urlinfo();
string[] strtemp = null;
urlinfo.host = "";
urlinfo.port = 80;
urlinfo.file = "/";
urlinfo.body = "";
int intindex = url.tolower().indexof("http://");
if (intindex != -1)
{
url = url.substring(7);
intindex = url.indexof("/");
if (intindex == -1)
{
urlinfo.host = url;
}
else
{
urlinfo.host = url.substring(0, intindex);
url = url.substring(intindex);
intindex = urlinfo.host.indexof(":");
if (intindex != -1)
{
strtemp = urlinfo.host.split(':');
urlinfo.host = strtemp[0];
int.tryparse(strtemp[1], out urlinfo.port);
}
intindex = url.indexof("?");
if (intindex == -1)
{
urlinfo.file = url;
}
else
{
strtemp = url.split('?');
urlinfo.file = strtemp[0];
urlinfo.body = strtemp[1];
}
}
}
return urlinfo;
}
/// <summary>
/// 发出请求并获取响应
/// </summary>
/// <param name="host"></param>
/// <param name="port"></param>
/// <param name="body"></param>
/// <param name="encode"></param>
/// <returns></returns>
private static string getresponse(string host, int port, string body, encoding encode)
{
string strresult = string.empty;
byte[] btesend = encoding.ascii.getbytes(body);
byte[] btereceive = new byte[1024];
int intlen = 0;
using (socket socket = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp))
{
try
{
socket.connect(host, port);
if (socket.connected)
{
socket.send(btesend, btesend.length, 0);
while ((intlen = socket.receive(btereceive, btereceive.length, 0)) > 0)
{
strresult += encode.getstring(btereceive, 0, intlen);
}
}
socket.close();
}
catch { }
}
return strresult;
}
/// <summary>
/// get请求
/// </summary>
/// <param name="url"></param>
/// <param name="encode"></param>
/// <returns></returns>
public static string get(string url, encoding encode)
{
urlinfo urlinfo = parseurl(url);
string strrequest = string.format("get {0}?{1} http/1.1\r\nhost:{2}:{3}\r\nconnection:close\r\n\r\n", urlinfo.file, urlinfo.body, urlinfo.host, urlinfo.port.tostring());
return getresponse(urlinfo.host, urlinfo.port, strrequest, encode);
}
/// <summary>
/// post请求
/// </summary>
/// <param name="url"></param>
/// <param name="encode"></param>
/// <returns></returns>
public static string post(string url, encoding encode)
{
urlinfo urlinfo = parseurl(url);
string strrequest = string.format("post {0} http/1.1\r\nhost:{1}:{2}\r\ncontent-length:{3}\r\ncontent-type:application/x-www-form-urlencoded\r\nconnection:close\r\n\r\n{4}", urlinfo.file, urlinfo.host, urlinfo.port.tostring(), urlinfo.body.length, urlinfo.body);
return getresponse(urlinfo.host, urlinfo.port, strrequest, encode);
}
}
}
调用示例:
复制代码 代码如下:
using system;
using system.text;
namespace consoleapplication1
{
//调用示例
class program
{
public static void main(string[] args)
{
console.writeline(webtoolkit.get("//www.jb51.net/t.asp?keyword=vbscript", encoding.default));
console.readkey();
}
}
}