c#使用Socket發(fā)送HTTP/HTTPS請(qǐng)求的實(shí)現(xiàn)代碼
C# 自帶的HttpWebRequest效率太低,對(duì)于自組HTTP封包不好操作。
在寫超級(jí)SQL注入工具時(shí),研究了很長一段時(shí)間如何使用Socket來發(fā)送HTTP、HTTPS請(qǐng)求。
經(jīng)過一年的修改和測(cè)試,可完美、高效發(fā)送并解析HTTP/HTTPS請(qǐng)求。修改過無數(shù)次bug。
在這里把核心代碼分享出來,供大家學(xué)習(xí)或做開發(fā)參考。
用這個(gè)代碼寫了一個(gè)簡單的HTTP發(fā)包工具。供大家參考。

工具下載:
核心類:HTTP.cs
using System;
using System.Collections.Generic;
using System.Text;
using tools;
using System.Net;
using System.Net.Sockets;
using System.IO.Compression;
using System.IO;
using System.Net.Security;
using System.Text.RegularExpressions;
using System.Threading;
using System.Diagnostics;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using HTTPTool;
namespace tools
{
public class HTTP
{
public const char T = '
';
public const String CT = "
";
public const String CTRL = "
";
public const String Content_Length_Str = "content-length: ";
public const String Content_Length_Str_M = "Content-Length: ";
public const String Content_Length = "content-length";
public const String Content_Encoding = "content-encoding";
public const String Transfer_Encoding = "transfer-encoding";
public const String Connection = "connection";
public static Main main = null;
public static long index = 0;
public void initMain(Main m)
{
main = m;
}
/**
*
發(fā)生異常嘗試重連
*
*/
public static ServerInfo sendRequestRetry(Boolean isSSL, int tryCount, String host, int port, String payload, String request, int timeout, String encoding, Boolean foward_302)
{
int count = 0;
Interlocked.Increment(ref index);
ServerInfo server = new ServerInfo();
timeout = timeout * 1000;
while (true)
{
if (count >= tryCount) break;
try
{
if (!isSSL)
{
server = sendHTTPRequest(count, host, port, payload, request, timeout, encoding, foward_302);
return server;
}
else
{
server = sendHTTPSRequest(count, host, port, payload, request, timeout, encoding, foward_302);
return server;
}
}
catch (Exception e)
{
Tools.SysLog("發(fā)包發(fā)生異常,正在重試----" + e.Message);
server.timeout = true;
continue;
}
finally
{
count++;
}
}
return server;
}
private static void checkContentLength(ref ServerInfo server, ref String request)
{
//重新計(jì)算并設(shè)置Content-length
int sindex = request.IndexOf(CTRL);
server.reuqestHeader = request;
if (sindex != -1)
{
server.reuqestHeader = request.Substring(0, sindex);
server.reuqestBody = request.Substring(sindex + 4, request.Length - sindex - 4);
int contentLength = Encoding.UTF8.GetBytes(server.reuqestBody).Length;
String newContentLength = Content_Length_Str_M + contentLength;
if (request.IndexOf(Content_Length_Str_M) != -1)
{
request = Regex.Replace(request, Content_Length_Str_M + "d+", newContentLength);
}
else
{
request = request.Insert(sindex, "
" + newContentLength);
}
}
else
{
request = Regex.Replace(request, Content_Length_Str + "d+", Content_Length_Str_M + "0");
request += CTRL;
}
}
private static void doHeader(ref ServerInfo server, ref String[] headers)
{
for (int i = 0; i < headers.Length; i++)
{
if (i == 0)
{
server.code = Tools.convertToInt(headers[i].Split(' ')[1]);
}
else
{
String[] kv = Regex.Split(headers[i], ": ");
String key = kv[0].ToLower();
if (!server.headers.ContainsKey(key))
{
//自動(dòng)識(shí)別編碼
if ("content-type".Equals(key))
{
String hecnode = getHTMLEncoding(kv[1], "");
if (!String.IsNullOrEmpty(hecnode))
{
server.encoding = hecnode;
}
}
if (kv.Length > 1)
{
server.headers.Add(key, kv[1]);
}
else
{
server.headers.Add(key, "");
}
}
}
}
}
private static ServerInfo sendHTTPRequest(int count, String host, int port, String payload, String request, int timeout, String encoding, Boolean foward_302)
{
String index = Thread.CurrentThread.Name + HTTP.index;
Stopwatch sw = new Stopwatch();
sw.Start();
ServerInfo server = new ServerInfo();
TcpClient clientSocket = null;
int sum = 0;
try
{
if (port > 0 && port <= 65556)
{
//編碼處理
server.request = request;
TimeOutSocket tos = new TimeOutSocket();
clientSocket = tos.Connect(host, port, timeout);
if (sw.ElapsedMilliseconds >= timeout)
{
return server;
}
clientSocket.SendTimeout = timeout - tos.useTime;
if (clientSocket.Connected)
{
checkContentLength(ref server, ref request);
server.request = request;
byte[] requestByte = Encoding.UTF8.GetBytes(request);
clientSocket.Client.Send(requestByte);
byte[] responseBody = new byte[1024 * 1000];
int len = 0;
//獲取header頭
String tmp = "";
StringBuilder sb = new StringBuilder();
clientSocket.ReceiveTimeout = timeout - (int)sw.ElapsedMilliseconds;
do
{
byte[] responseHeader = new byte[1];
len = clientSocket.Client.Receive(responseHeader, 1, SocketFlags.None);
if (len == 1)
{
char c = (char)responseHeader[0];
sb.Append(c);
if (c.Equals(T))
{
tmp = String.Concat(sb[sb.Length - 4], sb[sb.Length - 3], sb[sb.Length - 2], c);
}
}
} while (!tmp.Equals(CTRL) && sw.ElapsedMilliseconds < timeout);
server.header = sb.ToString().Replace(CTRL, "");
String[] headers = Regex.Split(server.header, CT);
if (headers != null && headers.Length > 0)
{
//處理header
doHeader(ref server, ref headers);
//自動(dòng)修正編碼
if (!String.IsNullOrEmpty(server.encoding))
{
encoding = server.encoding;
}
Encoding encod = Encoding.GetEncoding(encoding);
//302 301跳轉(zhuǎn)
if ((server.code == 302 || server.code == 301) && foward_302)
{
StringBuilder rsb = new StringBuilder(server.request);
int urlStart = server.request.IndexOf(" ") + 1;
int urlEnd = server.request.IndexOf(" HTTP");
if (urlStart != -1 && urlEnd != -1)
{
String url = server.request.Substring(urlStart, urlEnd - urlStart);
rsb.Remove(urlStart, url.Length);
String location = server.headers["location"];
if (!server.headers["location"].StartsWith("/") && !server.headers["location"].StartsWith("http"))
{
location = Tools.getCurrentPath(url) + location;
}
rsb.Insert(urlStart, location);
return sendHTTPRequest(count, host, port, payload, rsb.ToString(), timeout, encoding, false);
}
}
//根據(jù)請(qǐng)求頭解析
if (server.headers.ContainsKey(Content_Length))
{
int length = int.Parse(server.headers[Content_Length]);
while (sum < length && sw.ElapsedMilliseconds < timeout)
{
int readsize = length - sum;
len = clientSocket.Client.Receive(responseBody, sum, readsize, SocketFlags.None);
if (len > 0)
{
sum += len;
}
}
}
//解析chunked傳輸
else if (server.headers.ContainsKey(Transfer_Encoding))
{
//讀取長度
int chunkedSize = 0;
byte[] chunkedByte = new byte[1];
//讀取總長度
sum = 0;
do
{
String ctmp = "";
do
{
len = clientSocket.Client.Receive(chunkedByte, 1, SocketFlags.None);
ctmp += Encoding.UTF8.GetString(chunkedByte);
} while ((ctmp.IndexOf(CT) == -1) && (sw.ElapsedMilliseconds < timeout));
chunkedSize = Tools.convertToIntBy16(ctmp.Replace(CT, ""));
//chunked的結(jié)束0
是結(jié)束標(biāo)志,單個(gè)chunked塊
結(jié)束
if (ctmp.Equals(CT))
{
continue;
}
if (chunkedSize == 0)
{
//結(jié)束了
break;
}
int onechunkLen = 0;
while (onechunkLen < chunkedSize && sw.ElapsedMilliseconds < timeout)
{
len = clientSocket.Client.Receive(responseBody, sum, chunkedSize - onechunkLen, SocketFlags.None);
if (len > 0)
{
onechunkLen += len;
sum += len;
}
}
//判斷
} while (sw.ElapsedMilliseconds < timeout);
}
//connection close方式或未知body長度
else
{
while (sw.ElapsedMilliseconds < timeout)
{
if (clientSocket.Client.Poll(timeout, SelectMode.SelectRead))
{
if (clientSocket.Available > 0)
{
len = clientSocket.Client.Receive(responseBody, sum, (1024 * 200) - sum, SocketFlags.None);
if (len > 0)
{
sum += len;
}
}
else
{
break;
}
}
}
}
//判斷是否gzip
if (server.headers.ContainsKey(Content_Encoding))
{
server.body = unGzip(responseBody, sum, encod);
}
else
{
server.body = encod.GetString(responseBody, 0, sum);
}
}
}
}
}
catch (Exception e)
{
Exception ee = new Exception("HTTP發(fā)包錯(cuò)誤!錯(cuò)誤消息:" + e.Message + e.TargetSite.Name + "----發(fā)包編號(hào):" + index);
throw ee;
}
finally
{
sw.Stop();
server.length = sum;
server.runTime = (int)sw.ElapsedMilliseconds;
if (clientSocket != null)
{
clientSocket.Close();
}
}
return server;
}
private static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
}
private static ServerInfo sendHTTPSRequest(int count, String host, int port, String payload, String request, int timeout, String encoding, Boolean foward_302)
{
String index = Thread.CurrentThread.Name + HTTP.index;
Stopwatch sw = new Stopwatch();
sw.Start();
ServerInfo server = new ServerInfo();
int sum = 0;
TcpClient clientSocket = null; ;
try
{
if (port > 0 && port <= 65556)
{
TimeOutSocket tos = new TimeOutSocket();
clientSocket = tos.Connect(host, port, timeout);
if (sw.ElapsedMilliseconds >= timeout)
{
return server;
}
clientSocket.SendTimeout = timeout - tos.useTime;
SslStream ssl = null;
if (clientSocket.Connected)
{
ssl = new SslStream(clientSocket.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate));
SslProtocols protocol = SslProtocols.Ssl3 | SslProtocols.Ssl2 | SslProtocols.Tls;
ssl.AuthenticateAsClient(host, null, protocol, false);
if (ssl.IsAuthenticated)
{
checkContentLength(ref server, ref request);
server.request = request;
byte[] requestByte = Encoding.UTF8.GetBytes(request);
ssl.Write(requestByte);
ssl.Flush();
}
}
server.request = request;
byte[] responseBody = new byte[1024 * 1000];
int len = 0;
//獲取header頭
String tmp = "";
StringBuilder sb = new StringBuilder();
StringBuilder bulider = new StringBuilder();
clientSocket.ReceiveTimeout = timeout - (int)sw.ElapsedMilliseconds;
do
{
byte[] responseHeader = new byte[1];
int read = ssl.ReadByte();
char c = (char)read;
sb.Append(c);
if (c.Equals(T))
{
tmp = String.Concat(sb[sb.Length - 4], sb[sb.Length - 3], sb[sb.Length - 2], c);
}
} while (!tmp.Equals(CTRL) && sw.ElapsedMilliseconds < timeout);
server.header = sb.ToString().Replace(CTRL, "");
String[] headers = Regex.Split(server.header, CT);
//處理header
doHeader(ref server, ref headers);
//自動(dòng)修正編碼
if (!String.IsNullOrEmpty(server.encoding))
{
encoding = server.encoding;
}
Encoding encod = Encoding.GetEncoding(encoding);
//302 301跳轉(zhuǎn)
if ((server.code == 302 || server.code == 301) && foward_302)
{
int urlStart = server.request.IndexOf(" ");
int urlEnd = server.request.IndexOf(" HTTP");
if (urlStart != -1 && urlEnd != -1)
{
String url = server.request.Substring(urlStart + 1, urlEnd - urlStart - 1);
if (!server.headers["location"].StartsWith("/") && !server.headers["location"].StartsWith("https"))
{
server.request = server.request.Replace(url, Tools.getCurrentPath(url) + server.headers["location"]);
}
else
{
server.request = server.request.Replace(url, server.headers["location"]);
}
return sendHTTPSRequest(count, host, port, payload, server.request, timeout, encoding, false);
}
}
//根據(jù)請(qǐng)求頭解析
if (server.headers.ContainsKey(Content_Length))
{
int length = int.Parse(server.headers[Content_Length]);
while (sum < length && sw.ElapsedMilliseconds < timeout)
{
len = ssl.Read(responseBody, sum, length - sum);
if (len > 0)
{
sum += len;
}
}
}
//解析chunked傳輸
else if (server.headers.ContainsKey(Transfer_Encoding))
{
//讀取長度
int chunkedSize = 0;
byte[] chunkedByte = new byte[1];
//讀取總長度
sum = 0;
do
{
String ctmp = "";
do
{
len = ssl.Read(chunkedByte, 0, 1);
ctmp += Encoding.UTF8.GetString(chunkedByte);
} while (ctmp.IndexOf(CT) == -1 && sw.ElapsedMilliseconds < timeout);
chunkedSize = Tools.convertToIntBy16(ctmp.Replace(CT, ""));
//chunked的結(jié)束0
是結(jié)束標(biāo)志,單個(gè)chunked塊
結(jié)束
if (ctmp.Equals(CT))
{
continue;
}
if (chunkedSize == 0)
{
//結(jié)束了
break;
}
int onechunkLen = 0;
while (onechunkLen < chunkedSize && sw.ElapsedMilliseconds < timeout)
{
len = ssl.Read(responseBody, sum, chunkedSize - onechunkLen);
if (len > 0)
{
onechunkLen += len;
sum += len;
}
}
//判斷
} while (sw.ElapsedMilliseconds < timeout);
}
//connection close方式或未知body長度
else
{
while (sw.ElapsedMilliseconds < timeout)
{
if (clientSocket.Client.Poll(timeout, SelectMode.SelectRead))
{
if (clientSocket.Available > 0)
{
len = ssl.Read(responseBody, sum, (1024 * 200) - sum);
if (len > 0)
{
sum += len;
}
}
else
{
break;
}
}
}
}
//判斷是否gzip
if (server.headers.ContainsKey(Content_Encoding))
{
server.body = unGzip(responseBody, sum, encod);
}
else
{
server.body = encod.GetString(responseBody, 0, sum);
}
}
}
catch (Exception e)
{
Exception ee = new Exception("HTTPS發(fā)包錯(cuò)誤!錯(cuò)誤消息:" + e.Message + "----發(fā)包編號(hào):" + index);
throw ee;
}
finally
{
sw.Stop();
server.length = sum;
server.runTime = (int)sw.ElapsedMilliseconds;
if (clientSocket != null)
{
clientSocket.Close();
}
}
return server;
}
public static String unGzip(byte[] data, int len, Encoding encoding)
{
String str = "";
MemoryStream ms = new MemoryStream(data, 0, len);
GZipStream gs = new GZipStream(ms, CompressionMode.Decompress);
MemoryStream outbuf = new MemoryStream();
byte[] block = new byte[1024];
try
{
while (true)
{
int bytesRead = gs.Read(block, 0, block.Length);
if (bytesRead <= 0)
{
break;
}
else
{
outbuf.Write(block, 0, bytesRead);
}
}
str = encoding.GetString(outbuf.ToArray());
}
catch (Exception e)
{
Tools.SysLog("解壓Gzip發(fā)生異常----" + e.Message);
}
finally
{
outbuf.Close();
gs.Close();
ms.Close();
}
return str;
}
public static String getHTMLEncoding(String header, String body)
{
if (String.IsNullOrEmpty(header) && String.IsNullOrEmpty(body))
{
return "";
}
body = body.ToUpper();
Match m = Regex.Match(header, @"charsets*=s*""?(?<charset>[^""]*)", RegexOptions.IgnoreCase);
if (m.Success)
{
return m.Groups["charset"].Value.ToUpper();
}
else
{
if (String.IsNullOrEmpty(body))
{
return "";
}
m = Regex.Match(body, @"charsets*=s*""?(?<charset>[^""]*)", RegexOptions.IgnoreCase);
if (m.Success)
{
return m.Groups["charset"].Value.ToUpper();
}
}
return "";
}
}
}
以上就是關(guān)于c# Socket發(fā)送HTTP/HTTPS請(qǐng)求的核心代碼了,需要的朋友可以參考一下。
- SuperSocket封裝成C#類庫的步驟
- C# 實(shí)現(xiàn)WebSocket服務(wù)端教程
- 利用C#實(shí)現(xiàn)SSLSocket加密通訊的方法詳解
- C# Socket編程實(shí)現(xiàn)簡單的局域網(wǎng)聊天器的示例代碼
- C#使用Socket實(shí)現(xiàn)服務(wù)器與多個(gè)客戶端通信(簡單的聊天系統(tǒng))
- C#使用Socket實(shí)現(xiàn)心跳的方法示例
- C#SuperSocket的搭建并配置啟動(dòng)總結(jié)
- C#簡單實(shí)現(xiàn)發(fā)送socket字符串
- C#使用Socket實(shí)現(xiàn)局域網(wǎng)聊天
- C# websocket及時(shí)通信協(xié)議的實(shí)現(xiàn)方法示例
- C# .NET中Socket簡單實(shí)用框架的使用教程
- C# 通過Socket讀取大量數(shù)據(jù)的示例
相關(guān)文章
WinForm使用正則表達(dá)式提取內(nèi)容的方法示例
這篇文章主要介紹了WinForm使用正則表達(dá)式提取內(nèi)容的方法,結(jié)合實(shí)例形式分析了WinForm基于正則匹配獲取指定內(nèi)容的相關(guān)操作技巧,需要的朋友可以參考下2017-05-05
C#實(shí)現(xiàn)簡單屏幕監(jiān)控的方法
這篇文章主要介紹了C#實(shí)現(xiàn)簡單屏幕監(jiān)控的方法,涉及C#的圖標(biāo)隱藏及屏幕截圖等技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04
C#使用System.Net.Mail類實(shí)現(xiàn)郵件發(fā)送
這篇文章介紹了C#使用System.Net.Mail類實(shí)現(xiàn)郵件發(fā)送的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07
C#使用Socket實(shí)現(xiàn)發(fā)送和接收?qǐng)D片的方法
這篇文章主要介紹了C#使用Socket實(shí)現(xiàn)發(fā)送和接收?qǐng)D片的方法,涉及C#操作socket發(fā)送與接收文件的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04
C#實(shí)現(xiàn)String類型和json之間的相互轉(zhuǎn)換功能示例
這篇文章主要介紹了C#實(shí)現(xiàn)String類型和json之間的相互轉(zhuǎn)換功能,涉及C# json格式數(shù)據(jù)的構(gòu)造、轉(zhuǎn)換相關(guān)操作技巧,需要的朋友可以參考下2017-09-09

