


This article mainly introduces the implementation code of C# using Socket to send HTTP/HTTPS requests. Friends who need it can refer to
C# The built-in HttpWebRequest is too inefficient and is not easy to operate for self-organized HTTP packets. .
When writing a super SQL injection tool, I studied how to use Socket to send HTTP and HTTPS requests for a long time.
After one year of modification and testing, it can send and parse HTTP/HTTPS requests perfectly and efficiently. Fixed numerous bugs.
Share the core code here for everyone to learn or do development reference.
I used this code to write a simple HTTP packet sending tool. For reference.
Tool download:
HTTPTool.rar
Core class: 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 ""; } } }
The above is the detailed content of Detailed explanation of how C# uses Socket to send HTTP/HTTPS request examples. For more information, please follow other related articles on the PHP Chinese website!

一、基于TCP协议的socket套接字编程1、套接字工作流程先从服务器端说起。服务器端先初始化Socket,然后与端口绑定(bind),对端口进行监听(listen),调用accept阻塞,等待客户端连接。在这时如果有个客户端初始化一个Socket,然后连接服务器(connect),如果连接成功,这时客户端与服务器端的连接就建立了。客户端发送数据请求,服务器端接收请求并处理请求,然后把回应数据发送给客户端,客户端读取数据,最后关闭连接,一次交互结束,使用以下Python代码实现:importso

本篇文章给大家带来了关于php+socket的相关知识,其中主要介绍了IO多路复用,以及php+socket如何实现web服务器?感兴趣的朋友下面一起来看一下,希望对大家有帮助。

SpringBoot端第一步,引入依赖首先我们需要引入WebSocket所需的依赖,以及处理输出格式的依赖com.alibabafastjson1.2.73org.springframework.bootspring-boot-starter-websocket第二步,创建WebSocket配置类importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Config

当今人工智能(AI)技术的发展如火如荼,它们在各个领域都展现出了巨大的潜力和影响力。今天大姚给大家分享4个.NET开源的AI模型LLM相关的项目框架,希望能为大家提供一些参考。https://github.com/YSGStudyHards/DotNetGuide/blob/main/docs/DotNet/DotNetProjectPicks.mdSemanticKernelSemanticKernel是一种开源的软件开发工具包(SDK),旨在将大型语言模型(LLM)如OpenAI、Azure

php socket无法连接的解决办法:1、检查php是否开启socket扩展;2、打开php.ini文件,检查“php_sockets.dll”是否被加载;3、取消“php_sockets.dll”的注释状态即可。

本篇文章给大家带来了关于php+socket的相关知识,其中主要介绍了什么是socket?php+socket如何实现客户端与服务端数据传输?感兴趣的朋友下面一起来看一下,希望对大家有帮助。

随着互联网的发展,文件传输成为人们日常工作和娱乐中不可或缺的一部分。然而,传统的文件传输方式如邮件附件或文件分享网站存在一定的限制,无法满足实时性和安全性的需求。因此,利用PHP和Socket技术实现实时文件传输成为了一种新的解决方案。本文将介绍利用PHP和Socket技术实现实时文件传输的技术原理、优点和应用场景,并通过具体案例来演示该技术的实现方法。技术


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
