C#中使用Socket獲取網(wǎng)頁源代碼的代碼
更新時間:2010年12月22日 22:17:33 作者:
C#使用Socket獲取網(wǎng)頁源代碼的代碼,需要的朋友可以參考下。
WebToolkit類:
using System;
using System.Net.Sockets;
using System.Text;
namespace ConsoleApplication1
{
class WebToolkit
{
/// <summary>
/// Url結(jié)構(gòu)
/// </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>
/// 發(fā)出請求并獲取響應(yīng)
/// </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);
}
}
}
調(diào)用示例:
using System;
using System.Text;
namespace ConsoleApplication1
{
//調(diào)用示例
class Program
{
public static void Main(string[] args)
{
Console.WriteLine(WebToolkit.Get("http://www.dbjr.com.cn/t.asp?keyword=vbscript", Encoding.Default));
Console.ReadKey();
}
}
}
復(fù)制代碼 代碼如下:
using System;
using System.Net.Sockets;
using System.Text;
namespace ConsoleApplication1
{
class WebToolkit
{
/// <summary>
/// Url結(jié)構(gòu)
/// </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>
/// 發(fā)出請求并獲取響應(yīng)
/// </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);
}
}
}
調(diào)用示例:
復(fù)制代碼 代碼如下:
using System;
using System.Text;
namespace ConsoleApplication1
{
//調(diào)用示例
class Program
{
public static void Main(string[] args)
{
Console.WriteLine(WebToolkit.Get("http://www.dbjr.com.cn/t.asp?keyword=vbscript", Encoding.Default));
Console.ReadKey();
}
}
}
相關(guān)文章
c# 根據(jù)NPOI 讀取一個excel 文件的多個Sheet
這篇文章主要介紹了c# 根據(jù)NPOI 讀取一個excel 文件的多個Sheet,幫助大家更好的利用c#處理excel表格,感興趣的朋友可以了解下2020-12-12淺談C#跨線程調(diào)用窗體控件(比如TextBox)引發(fā)的線程安全問題
下面小編就為大家分享一篇淺談C#跨線程調(diào)用窗體控件(比如TextBox)引發(fā)的線程安全問題,具有很好的參考價值,希望對大家有所幫助2017-11-11C#實現(xiàn)網(wǎng)絡(luò)通信共享庫NetShare的使用示例
本文主要介紹了C#實現(xiàn)網(wǎng)絡(luò)通信共享庫NetShare,網(wǎng)絡(luò)通信共享庫NetShare用于保證客戶端與服務(wù)器通信數(shù)據(jù)包的規(guī)范和統(tǒng)一,感興趣的可以了解一下2023-11-11C#使用SQL Dataset數(shù)據(jù)集代碼實例
今天小編就為大家分享一篇關(guān)于的文章,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10