這篇文章主要介紹了c#使用Socket發送HTTP/HTTPS請求的實現代碼,需要的朋友可以參考下
C# 自帶的HttpWebRequest效率太低,對於自組HTTP數據包不好操作。
在寫超級SQL注入工具時,研究了很長一段時間如何使用Socket來發送HTTP、HTTPS請求。
經過一年的修改和測試,可完美、有效率地發送並解析HTTP/HTTPS請求。修改過無數次bug。
在這裡把核心程式碼分享出來,讓大家學習或做開發參考。
用這個程式碼寫了一個簡單的HTTP發包工具。供大家參考。
工具下載:
HTTPTool.rar
核心類別: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; } /** * 发生异常尝试重连 * */ 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("发包发生异常,正在重试----" + e.Message); server.timeout = true; continue; } finally { count++; } } return server; } private static void checkContentLength(ref ServerInfo server, ref String request) { //重新计算并设置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)) { //自动识别编码 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); //自动修正编码 if (!String.IsNullOrEmpty(server.encoding)) { encoding = server.encoding; } Encoding encod = Encoding.GetEncoding(encoding); //302 301跳转 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); } } //根据请求头解析 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的结束0 是结束标志,单个chunked块 结束 if (ctmp.Equals(CT)) { continue; } if (chunkedSize == 0) { //结束了 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发包错误!错误消息:" + e.Message + e.TargetSite.Name + "----发包编号:" + 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); //自动修正编码 if (!String.IsNullOrEmpty(server.encoding)) { encoding = server.encoding; } Encoding encod = Encoding.GetEncoding(encoding); //302 301跳转 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); } } //根据请求头解析 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的结束0 是结束标志,单个chunked块 结束 if (ctmp.Equals(CT)) { continue; } if (chunkedSize == 0) { //结束了 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发包错误!错误消息:" + e.Message + "----发包编号:" + 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发生异常----" + 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 ""; } } }
以上是C#如何使用Socket發送HTTP/HTTPS請求實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

c#.netissutableforenterprise-levelapplications withemofrosoftecosystemdueToItsStrongTyping,richlibraries,androbustperraries,androbustperformance.however,itmaynotbeidealfoross-platement forment forment forment forvepentment offependment dovelopment toveloperment toveloperment whenrawspeedsportor whenrawspeedseedpolitical politionalitable,

C#在.NET中的編程過程包括以下步驟:1)編寫C#代碼,2)編譯為中間語言(IL),3)由.NET運行時(CLR)執行。 C#在.NET中的優勢在於其現代化語法、強大的類型系統和與.NET框架的緊密集成,適用於從桌面應用到Web服務的各種開發場景。

C#是一種現代、面向對象的編程語言,由微軟開發並作為.NET框架的一部分。 1.C#支持面向對象編程(OOP),包括封裝、繼承和多態。 2.C#中的異步編程通過async和await關鍵字實現,提高應用的響應性。 3.使用LINQ可以簡潔地處理數據集合。 4.常見錯誤包括空引用異常和索引超出範圍異常,調試技巧包括使用調試器和異常處理。 5.性能優化包括使用StringBuilder和避免不必要的裝箱和拆箱。

C#.NET應用的測試策略包括單元測試、集成測試和端到端測試。 1.單元測試確保代碼的最小單元獨立工作,使用MSTest、NUnit或xUnit框架。 2.集成測試驗證多個單元組合的功能,常用模擬數據和外部服務。 3.端到端測試模擬用戶完整操作流程,通常使用Selenium進行自動化測試。

C#高級開發者面試需要掌握異步編程、LINQ、.NET框架內部工作原理等核心知識。 1.異步編程通過async和await簡化操作,提升應用響應性。 2.LINQ以SQL風格操作數據,需注意性能。 3..NET框架的CLR管理內存,垃圾回收需謹慎使用。

C#.NET面試問題和答案包括基礎知識、核心概念和高級用法。 1)基礎知識:C#是微軟開發的面向對象語言,主要用於.NET框架。 2)核心概念:委託和事件允許動態綁定方法,LINQ提供強大查詢功能。 3)高級用法:異步編程提高響應性,表達式樹用於動態代碼構建。

C#.NET是構建微服務的熱門選擇,因為其生態系統強大且支持豐富。 1)使用ASP.NETCore創建RESTfulAPI,處理訂單創建和查詢。 2)利用gRPC實現微服務間的高效通信,定義和實現訂單服務。 3)通過Docker容器化微服務,簡化部署和管理。

C#和.NET的安全最佳實踐包括輸入驗證、輸出編碼、異常處理、以及身份驗證和授權。 1)使用正則表達式或內置方法驗證輸入,防止惡意數據進入系統。 2)輸出編碼防止XSS攻擊,使用HttpUtility.HtmlEncode方法。 3)異常處理避免信息洩露,記錄錯誤但不返回詳細信息給用戶。 4)使用ASP.NETIdentity和Claims-based授權保護應用免受未授權訪問。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

SublimeText3漢化版
中文版,非常好用

WebStorm Mac版
好用的JavaScript開發工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器