时间:2021-05-20
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("https:///t.asp?keyword=vbscript", Encoding.Default));
Console.ReadKey();
}
}
}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了C#获取网页源代码的方法。分享给大家供大家参考。具体如下:publicstringGetPageHTML(stringurl){try{HttpW
最近在做一个项目,其中一个功能是根据一个URL地址,获取到网页的源代码。在ASP.NET(C#)中,获取网页源代码貌似有很多种方法,我随便搞了一个简单的WebC
利用C#获取计算机名,IP,MAC信息,如下为源代码:复制代码代码如下:usingSystem;usingSystem.Collections.Generic;
本文演示了在C#中使用画笔的源代码,供大家参考使用。下面的源代码中首先定义了一个矩形类(Rectangle),这个类从Shape类中派生出来,在类中定义了两个变
如何从C#获取字符串中汉字的个数?C#中使用正则表达式来从字符串中判断出汉字,然后计数,从而得到字符串中的汉字个数。先看这段代码:复制代码代码如下://首先引用