C#中使用Socket获取网页源代码的代码
程序员文章站
2023-01-09 22:32:00
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();
}
}
}
上一篇: C#排序算法之快速排序
推荐阅读
-
C#使用Protocol Buffer(ProtoBuf)进行Unity中的Socket通信
-
Python解析网页源代码中的115网盘链接实例
-
C#中使用Socket获取网页源代码的代码
-
Android中获取网页表单中的数据实现思路及代码
-
【转载】C#中List集合使用GetRange方法获取指定索引范围内的所有值
-
在C#中List集合使用First()方法获取第一个元素的操作
-
再编写代码中报错:CS8107 C# 7.0 中不支持功能“xxxxxx”。请使用 7.1 或更高的语言版本。
-
WPF中Style文件的引用——使用xaml代码或者C#代码动态加载
-
WPF中Style文件的引用——使用xaml代码或者C#代码动态加载
-
【转载】 C#中使用Count方法获取List集合中符合条件的个数