GrassWebProxy

GrassWebProxy第一版:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;
using System.Net.Http;
using System.Runtime.InteropServices.ComTypes;

namespace GrassWebProxy
{
    public class Ticket
    {
        public ulong TicketNo { get; set; }
        public string BeginDateTime {  get; set; }
        public string EndDateTime { get; set; }
    }
    public class GP
    {
        public static bool CheckFlag(ref byte[] msg)
        {
            //"G" 71 0x47
            //"P" 80 0x50
            if (msg[0]==0x47 && msg[1]==0x50)
            {
                Console.WriteLine("CheckFlag OK");
                return true;
            }
            return false;
        }
        static public Ticket ReadConfig(string cfg = "Ticket1.json")
        {
            // 读取文件内容  
            string jsonContent = File.ReadAllText(cfg);

            // 反序列化JSON字符串到对象  
            Ticket ticket = JsonConvert.DeserializeObject<Ticket>(jsonContent);
            // 输出结果  
            Console.WriteLine($"{ticket.TicketNo}\r\n{ticket.BeginDateTime}---{ticket.EndDateTime}");
            return ticket;
        }
        static public bool CheckTicket(ref byte[] msg, Ticket counterfoil)
        {
            ulong ticket = (ulong)((msg[2]<< 56) | (msg[3] << 48) | (msg[4] << 40)
                | (msg[5] << 32) | (msg[6] << 24) | (msg[7] <<16)|(msg[8] <<8)|msg[9]);
            Console.WriteLine(ticket.ToString("X"));
            if(counterfoil.TicketNo==ticket)
            {
                return true;
            }
            return false;
        }
    }
    class TcpServer
    {
        private TcpListener tcpListener;
        private Thread listenThread;

        public TcpServer(string ipAddress, int port)
        {
            this.tcpListener = new TcpListener(IPAddress.Parse(ipAddress), port);
            this.listenThread = new Thread(new ThreadStart(ListenForClients));
            this.listenThread.Start();
        }

        private void ListenForClients()
        {
            this.tcpListener.Start();

            while (true)
            {
                // 阻塞直到客户端连接  
                TcpClient client = this.tcpListener.AcceptTcpClient();

                // 创建一个新的线程来处理客户端通信  
                Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
                clientThread.Start(client);
            }
        }



        /// <summary>
        /// 注意接受缓存只有4096字节。
        /// </summary>
        /// <param name="client"></param>
        private async void HandleClientComm(object client)
        {
            TcpClient tcpClient = (TcpClient)client;
            NetworkStream clientStream = tcpClient.GetStream();

            //GuestIP.RecordIpToDatabase(tcpClient);

            byte[] message = new byte[8192];
            int bytesRead;

            while (true)
            {
                bytesRead = 0;

                try
                {
                    // 阻塞直到客户端发送数据  
                    bytesRead = clientStream.Read(message, 0, message.Length);

                    if (bytesRead == 0)
                    {
                        // 客户端关闭了连接  
                        break;
                    }

                    Console.WriteLine($"Read Bytes:{bytesRead}");
                }
                catch
                {
                    // 客户端断开了连接  
                    break;
                }

                try
                {
                    //Check
                    if (GP.CheckFlag(ref message) == true && bytesRead >= 10)
                    {
                        Console.WriteLine("GrassWebProxy应用来访");

                        //Check Ticket
                        if(GP.CheckTicket(ref message,GP.ReadConfig("Ticket1.json"))==true)
                        {
                            Console.WriteLine("检票完成");

                            var data = new string(Encoding.UTF8.GetChars(message, 10, bytesRead-10));
                            Console.WriteLine(data);

                            
                            using (var httpClient = new HttpClient())
                            {
                                // 注意:这里需要处理完整的HTTP请求,包括头部和正文  
                                // 这里仅作为示例,我们实际上并没有发送整个请求  
                                var response = await httpClient.GetAsync(data);

                                var responseContent = await response.Content.ReadAsStringAsync();
                                Console.WriteLine(responseContent);
                                // 将响应写回客户端  
                                var responseBytes = Encoding.UTF8.GetBytes(responseContent);
                                await clientStream.WriteAsync(responseBytes, 0, responseBytes.Length);
                                Console.WriteLine("Send {0} Bytes.", responseBytes.Length);
                            }

                        }
                        goto End;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"{ex.Message}");
                }

                // 将接收到的数据回显给客户端  
                ASCIIEncoding encoder = new ASCIIEncoding();
                string messageString = encoder.GetString(message, 0, bytesRead);
                Console.WriteLine("Received from client: " + messageString);

                byte[] buffer = encoder.GetBytes(messageString);

                // 发送回显数据给客户端  
                clientStream.Write(buffer, 0, buffer.Length);
                clientStream.Flush();
            End:
                //    //查看来访IP信息
                //    GuestIP.ReadLastGuestIP();
                ;
            }

            tcpClient.Close();
        }
    }
    internal class Program
    {
        static void Main(string[] args)
        {
            const int port = 8422;
            TcpServer server = new TcpServer("127.0.0.1", port);

            Console.WriteLine($"TCP Server listening on port {port}...");

            // 阻止主线程结束,直到用户手动停止  
            Console.ReadLine();
        }
    }
}
Ticket1.json 文件内容:
{
  "TicketNo":1,
  "BeginDateTime": "20240728T20:30:30",
  "EndDateTime": "20240731T20:30:30"
}

GrassWebProxyV2:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;
using System.Net;
using System.Net.Http;

namespace GrassWebProxyV2
{

    public class Ticket
    {
        public ulong TicketNo { get; set; }
        public string BeginDateTime { get; set; }
        public string EndDateTime { get; set; }
    }
    public class GP
    {
        public static bool CheckFlag(ref byte[] msg)
        {
            //"G" 71 0x47
            //"P" 80 0x50
            if (msg[0] == 0x47 && msg[1] == 0x50)
            {
                Console.WriteLine("CheckFlag OK");
                return true;
            }
            return false;
        }
        static public Ticket ReadConfig(string cfg = "Ticket1.json")
        {
            // 读取文件内容  
            string jsonContent = File.ReadAllText(cfg);

            // 反序列化JSON字符串到对象  
            Ticket ticket = JsonConvert.DeserializeObject<Ticket>(jsonContent);
            // 输出结果  
            Console.WriteLine($"{ticket.TicketNo}\r\n{ticket.BeginDateTime}---{ticket.EndDateTime}");
            return ticket;
        }
        static public bool CheckTicket(ref byte[] msg, Ticket counterfoil)
        {
            /*ulong ticket = (ulong)((msg[2] << 56) | (msg[3] << 48) | (msg[4] << 40)
                | (msg[5] << 32) | (msg[6] << 24) | (msg[7] << 16) | (msg[8] << 8) | msg[9]);*/
            ulong ticket = BitConverter.ToUInt64(msg, 2); // 从索引2开始读取8个字节
            //Console.WriteLine(ticket.ToString("X"));
            if (counterfoil.TicketNo == ticket)
            {
                return true;
            }
            return false;
        }
        static public bool CheckFlagAdTicket(ref byte[] msg, Ticket counterfoil)
        {
            //"G" 71 0x47
            //"P" 80 0x50
            ulong ticket = BitConverter.ToUInt64(msg, 2); // 从索引2开始读取8个字节
            if (msg[0] == 0x47 && msg[1] == 0x50 && counterfoil.TicketNo == ticket)
            {
                return true;
            }
            return false;
        }
    }

    class SimpleWebProxy
    {
        private HttpListener listener;

        public SimpleWebProxy(int port)
        {
            // 初始化HttpListener,监听所有传入HTTP请求  
            listener = new HttpListener();
            listener.Prefixes.Add($"http://+:{port}/");
            listener.Start();

            Console.WriteLine($"Grass Web proxy listening on port {port}...");

            // 开始异步处理请求  
            Task.Run(() => ListenForRequests());
        }

        private async Task ListenForRequests()
        {
            while (true)
            {
                // 等待并获取下一个客户端连接  
                HttpListenerContext context = await listener.GetContextAsync();
                HttpListenerRequest request = context.Request;

                 假设Ticket存储在HTTP头中,名为"X-Proxy-Ticket"  
                string ticketHeader = request.Headers["X-Proxy-Ticket"];
                // 假设您有一个方法CheckTicket(string ticket)来验证ticket  
                // return CheckTicket(ticketHeader); // 这里应该是您的验证逻辑  
                // 为了示例,我们直接返回true或false(这里总是返回false以模拟验证失败)
                // 使用UTF8编码将字符串转换为byte[]  
                byte[] ticketBytes = Encoding.UTF8.GetBytes(ticketHeader);

                var counterfoil = GP.ReadConfig("Ticket1.json");
                if(GP.CheckFlagAdTicket(ref ticketBytes, counterfoil)==true)
                {
                    // 获取请求的URL(不包含查询字符串)  
                    string url = request.Url.Scheme + "://" + request.Url.Host + ":" + request.Url.Port + request.Url.AbsolutePath;
                    Console.WriteLine(url);
                    // 创建一个WebRequest到目标URL  
                    HttpWebRequest proxyRequest = (HttpWebRequest)WebRequest.Create(url);

                    // 复制请求方法(如GET、POST)  
                    proxyRequest.Method = request.HttpMethod;

                    // 如果需要,可以添加更多的请求头(这里未添加)  

                    // 发送请求到目标服务器并获取响应  
                    using (HttpWebResponse proxyResponse = (HttpWebResponse)await proxyRequest.GetResponseAsync())
                    {
                        // 将响应转发给客户端  
                        using (Stream responseStream = proxyResponse.GetResponseStream())
                        using (Stream outputStream = context.Response.OutputStream)
                        {
                            // 设置响应的HTTP状态码和状态描述  
                            context.Response.StatusCode = (int)proxyResponse.StatusCode;
                            context.Response.StatusDescription = proxyResponse.StatusDescription;

                            // 遍历所有响应头并复制到响应中  
                            foreach (string headerKey in proxyResponse.Headers.AllKeys)
                            {
                                if (headerKey != "Transfer-Encoding" &&
                                    !(context.Response.Headers.AllKeys.Contains(headerKey)))
                                {
                                    context.Response.AddHeader(headerKey, proxyResponse.Headers[headerKey]);
                                }
                            }

                            // 读取响应流并将其写入客户端的输出流  
                            byte[] buffer = new byte[4096];
                            int bytesRead;
                            while ((bytesRead = await responseStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
                            {
                                await outputStream.WriteAsync(buffer, 0, bytesRead);
                            }
                        }
                    }
                }
                else
                {
                    // 票据验证失败,可以发送错误响应给客户端  
                    context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                    context.Response.Close();
                }
            }
        }
    }
    internal class Program
    {
        static void Main(string[] args)
        {
            new SimpleWebProxy(8422);

            // 防止主线程退出  
            Console.WriteLine("Press Enter to exit...");
            Console.ReadLine();
        }
    }
}

GrassWebProxyV4:

// See https://aka.ms/new-console-template for more information
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Net.Http;
using System.Runtime.InteropServices.ComTypes;
using System.IO.Compression;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Runtime.Serialization;

namespace GrassWebProxy
{
    public class CustomDateTimeConverter : IsoDateTimeConverter
    {
        public CustomDateTimeConverter()
        {
            DateTimeFormat = "yyyyMMdd'T'HH:mm:ss";
        }
    }
    public class Ticket
    {
        public ulong TicketNo { get; set; }

        [JsonConverter(typeof(CustomDateTimeConverter))]
        public DateTime BeginDateTime { get; set; }

        [JsonConverter(typeof(CustomDateTimeConverter))]
        public DateTime EndDateTime { get; set; }
    }
    public class GP
    {
        public static bool CheckFlag(ref byte[] msg)
        {
            //"G" 71 0x47
            //"P" 80 0x50
            if (msg[0] == 0x47 && msg[1] == 0x50)
            {
                Console.WriteLine("CheckFlag OK");
                return true;
            }
            return false;
        }
        static public Ticket ReadConfig(string cfg = "Ticket1.json")
        {
            // 读取文件内容  
            string jsonContent = File.ReadAllText(cfg);

            // 反序列化JSON字符串到对象  
            Ticket ticket = JsonConvert.DeserializeObject<Ticket>(jsonContent);
            // 输出结果  
            Console.WriteLine($"{ticket.TicketNo}\r\n{ticket.BeginDateTime}---{ticket.EndDateTime}");
            return ticket;
        }
        static public bool CheckTicket(ref byte[] msg, Ticket counterfoil)
        {
            ulong ticket = (ulong)((msg[2] << 56) | (msg[3] << 48) | (msg[4] << 40)
                | (msg[5] << 32) | (msg[6] << 24) | (msg[7] << 16) | (msg[8] << 8) | msg[9]);
            Console.WriteLine(ticket.ToString("X"));
            if (counterfoil.TicketNo == ticket)
            {
                return true;
            }
            return false;
        }

        public static byte[] Compress(byte[] input)
        {
            using (var memoryStream = new MemoryStream())
            {
                using (var gzipStream = new GZipStream(memoryStream, CompressionMode.Compress, true))
                {
                    gzipStream.Write(input, 0, input.Length);
                }

                // 确保所有数据都被写入并压缩  
                return memoryStream.ToArray();
            }
        }
        public static byte[] DecompressGzip(byte[] gzip, int prebytes = 4096)
        {
            using (var ms = new MemoryStream(gzip))
            {
                using (var gzipStream = new GZipStream(ms, CompressionMode.Decompress))
                {
                    var buffer = new byte[prebytes];
                    using (var memoryStreamOut = new MemoryStream())
                    {
                        int read;
                        while ((read = gzipStream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            memoryStreamOut.Write(buffer, 0, read);
                        }
                        return memoryStreamOut.ToArray();
                    }
                }
            }
        }
    }
    class TcpServer
    {
        private TcpListener tcpListener;
        private Thread listenThread;

        public TcpServer(string ipAddress, int port)
        {
            this.tcpListener = new TcpListener(IPAddress.Parse(ipAddress), port);
            this.listenThread = new Thread(new ThreadStart(ListenForClients));
            this.listenThread.Start();
        }

        private void ListenForClients()
        {
            this.tcpListener.Start();

            while (true)
            {
                // 阻塞直到客户端连接  
                TcpClient client = this.tcpListener.AcceptTcpClient();

                // 创建一个新的线程来处理客户端通信  
                Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
                clientThread.Start(client);
            }
        }



        /// <summary>
        /// 注意接受缓存只有4096字节。
        /// </summary>
        /// <param name="client"></param>
        private async void HandleClientComm(object client)
        {
            TcpClient tcpClient = (TcpClient)client;
            NetworkStream clientStream = tcpClient.GetStream();

            //GuestIP.RecordIpToDatabase(tcpClient);

            byte[] message = new byte[8192];
            int bytesRead;

            while (true)
            {
                bytesRead = 0;

                try
                {
                    // 阻塞直到客户端发送数据  
                    bytesRead = clientStream.Read(message, 0, message.Length);

                    if (bytesRead == 0)
                    {
                        // 客户端关闭了连接  
                        break;
                    }

                    Console.WriteLine($"Read Bytes:{bytesRead}");
                }
                catch
                {
                    // 客户端断开了连接  
                    break;
                }

                try
                {
                    //Check
                    if (GP.CheckFlag(ref message) == true && bytesRead >= 10)
                    {
                        Console.WriteLine("GrassWebProxy应用来访");
                        //Check Ticket
                        if (GP.CheckTicket(ref message, GP.ReadConfig("Ticket1.json")) == true)
                        {
                            Console.WriteLine("检票完成");

                            var data = new string(Encoding.UTF8.GetChars(message, 10, bytesRead - 10));
                            Console.WriteLine(data);


                            using (var httpClient = new HttpClient())
                            {
                                // 注意:这里需要处理完整的HTTP请求,包括头部和正文  
                                // 这里仅作为示例,我们实际上并没有发送整个请求  
                                var response = await httpClient.GetAsync(data);

                                var responseContent = await response.Content.ReadAsStringAsync();
                                Console.WriteLine(responseContent);
                                // 将响应写回客户端  
                                //var responseBytes = Encoding.UTF8.GetBytes(responseContent);
                                var rsbuf = await response.Content.ReadAsByteArrayAsync();
                                Console.WriteLine(rsbuf.Length);
                                var gzipbuf = GP.Compress(rsbuf);
                                Console.WriteLine(gzipbuf.Length);
                                await clientStream.WriteAsync(gzipbuf, 0, gzipbuf.Length);
                                Console.WriteLine("Send {0} Bytes.", gzipbuf.Length);
                                clientStream.Flush();
                                Console.WriteLine("END");
                                tcpClient.Close();
                            }

                        }
                        goto End;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"{ex.Message}");
                }

                // 将接收到的数据回显给客户端  
                ASCIIEncoding encoder = new ASCIIEncoding();
                string messageString = encoder.GetString(message, 0, bytesRead);
                Console.WriteLine("Received from client: " + messageString);

                byte[] buffer = encoder.GetBytes(messageString);

                // 发送回显数据给客户端  
                clientStream.Write(buffer, 0, buffer.Length);
                clientStream.Flush();
                Console.WriteLine("END");
                tcpClient.Close();
            End:
                //    //查看来访IP信息
                //    GuestIP.ReadLastGuestIP();
                ;
            }
            
        }
    }
    internal class Program
    {
        static void Main(string[] args)
        {
            const int port = 8422;
            TcpServer server = new TcpServer("127.0.0.1", port);

            Console.WriteLine($"TCP Server listening on port {port}...");

            // 阻止主线程结束,直到用户手动停止  
            Console.ReadLine();
        }
    }
}

GrassWebProxyClient:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;
using System.Net.Http;
using System.Net;
using System.Net.Sockets;

namespace GrassWebProxyClient
{
    public class IPport
    {
        public string IP { get; set; }
        public int Port { get; set; }
    }
    public class Ticket
    {
        public ulong TicketNo { get; set; }
        public string BeginDateTime { get; set; }
        public string EndDateTime { get; set; }
    }
    internal class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string jsonFilePath = "ipport.json"; // 替换为你的JSON文件路径  

                // 读取文件内容  
                string jsonContent = File.ReadAllText(jsonFilePath);

                // 反序列化JSON字符串到对象  
                IPport ipaddr = JsonConvert.DeserializeObject<IPport>(jsonContent);

                // 输出结果  
                Console.WriteLine($"{ipaddr.IP}:{ipaddr.Port}");

                // 创建一个TcpClient实例并连接到服务器  
                TcpClient client = new TcpClient($"{ipaddr.IP}", ipaddr.Port);

                // 获取一个NetworkStream对象以进行读写  
                NetworkStream stream = client.GetStream();

                jsonFilePath = "Ticket1.json";
                // 读取文件内容  
                jsonContent = File.ReadAllText(jsonFilePath);
                // 反序列化JSON字符串到对象  
                Ticket ticket = JsonConvert.DeserializeObject<Ticket>(jsonContent);

                // 输出结果  
                Console.WriteLine($"{ticket.TicketNo}\r\n{ticket.BeginDateTime}---{ticket.EndDateTime}");
                //组装
                byte[] data = { 0x47, 0x50, 0x01, 0xF2, 0x00, 0x00, 0x00, 0x01,0x01,0x01 };
                for (int i=2,j=7;i<data.Length;i++,j--)
                {
                    data[i] = (byte)(ticket.TicketNo>>(j*8));
                    //Console.WriteLine(data[i]);
                }
                // 发送消息到服务器  
                stream.Write(data, 0, data.Length);

                // 发送消息到服务器
                // 将消息转换为字节数组  
                string message = "http://www.cjors.cn/";
                byte[] requestdata = Encoding.UTF8.GetBytes(message);
                // 将 data 和 requestdata 合并到 buffer 中  
                byte[] buffer = new byte[data.Length + requestdata.Length];
                Buffer.BlockCopy(data, 0, buffer, 0, data.Length);
                Buffer.BlockCopy(requestdata, 0, buffer, data.Length, requestdata.Length);
                stream.Write(requestdata, 0, requestdata.Length);

                // 读取服务器的响应  
                byte[] buf = new byte[8192];
                int bytesRead = stream.Read(buf, 0, buf.Length);

                // 将接收到的字节转换为字符串  
                string responseData = Encoding.UTF8.GetString(buf, 0, bytesRead);
                Console.WriteLine("Received from server: " + responseData);
                Console.WriteLine("ReceivedBytes:{0}", bytesRead);

                //将Json写入文件
                File.WriteAllText("response.html", responseData);

                // 关闭连接  
                client.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e.Message);
            }

            // 等待用户按键,以便在控制台中查看结果  
            Console.WriteLine("Press Enter to continue...");
            Console.ReadLine();
        }
    }
}

GrassWebProxyClientV2:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using System.Net.Sockets;
using Newtonsoft.Json;
using System.Security.Policy;

namespace GrassWebProxyClientV2
{
    public class IPport
    {
        public string IP { get; set; }
        public int Port { get; set; }
    }
    public class CpolarInfo
    {
        public int total { get; set; }
        public item[] items { get; set; }
    }
    public class item
    {
        public string name { get; set; }
        public string create_datetime { get; set; }
        public string pubulic_url { get; set; }
    }
    public class Ticket
    {
        public ulong TicketNo { get; set; }
        public string BeginDateTime { get; set; }
        public string EndDateTime { get; set; }
    }
    internal class Program
    {
        static void GetCploarInfo()
        {
            try
            {
                string jsonFilePath = "ipport.json"; // 替换为你的JSON文件路径  

                // 读取文件内容  
                string jsonContent = File.ReadAllText(jsonFilePath);

                // 反序列化JSON字符串到对象  
                IPport ipaddr = JsonConvert.DeserializeObject<IPport>(jsonContent);

                // 输出结果  
                Console.WriteLine($"{ipaddr.IP}:{ipaddr.Port}");

                // 创建一个TcpClient实例并连接到服务器  
                TcpClient client = new TcpClient($"{ipaddr.IP}", ipaddr.Port);

                // 获取一个NetworkStream对象以进行读写  
                NetworkStream stream = client.GetStream();

                // 将消息转换为字节数组  
                //string message = "Hello from the client!";
                //byte[] data = Encoding.ASCII.GetBytes(message);
                byte[] data = { 0x4E, 0x74, 0x01, 0xF2, 0x00, 0x00, 0x00, 0x01 };

                // 发送消息到服务器  
                stream.Write(data, 0, data.Length);

                // 读取服务器的响应  
                byte[] buffer = new byte[4096];
                int bytesRead = stream.Read(buffer, 0, buffer.Length);

                // 将接收到的字节转换为字符串  
                string responseData = Encoding.ASCII.GetString(buffer, 0, bytesRead);
                Console.WriteLine("Received from server: " + responseData);
                Console.WriteLine("ReceivedBytes:{0}", bytesRead);
                // 关闭连接  
                client.Close();
                //将Json写入文件
                File.WriteAllText("cpolarinfo.json", responseData);


            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e.Message);
            }
        }
        static void UseWebProxy(string webProxyUrl,string ip,int port)
        {
            try
            {
                // 创建一个TcpClient实例并连接到服务器  
                TcpClient client = new TcpClient(ip, port);

                // 获取一个NetworkStream对象以进行读写  
                NetworkStream stream = client.GetStream();

                string jsonFilePath = "Ticket1.json";
                // 读取文件内容  
                string jsonContent = File.ReadAllText(jsonFilePath);
                // 反序列化JSON字符串到对象  
                Ticket ticket = JsonConvert.DeserializeObject<Ticket>(jsonContent);

                // 输出结果  
                Console.WriteLine($"{ticket.TicketNo}\r\n{ticket.BeginDateTime}---{ticket.EndDateTime}");
                //组装
                byte[] data = { 0x47, 0x50, 0x01, 0xF2, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01 };
                for (int i = 2, j = 7; i < data.Length; i++, j--)
                {
                    data[i] = (byte)(ticket.TicketNo >> (j * 8));
                    //Console.WriteLine(data[i]);
                }
                
                // 将消息转换为字节数组  
                // 将 data 和 requestdata 合并到 buffer 中  
                byte[] requestdata = Encoding.UTF8.GetBytes(webProxyUrl);
                byte[] buf = new byte[data.Length + requestdata.Length];
                Buffer.BlockCopy(data, 0, buf, 0, data.Length);
                Buffer.BlockCopy(requestdata, 0, buf, data.Length, requestdata.Length);

                // 发送消息到服务器
                stream.Write(buf, 0, buf.Length);

                // 读取服务器的响应  
                byte[] buffer = new byte[81920];
                //int bytesRead = stream.Read(buffer, 0, buffer.Length);
                int totalBytesRead = 0;
                int bytesRead = 0;
                string responseData = string.Empty;

                while ((bytesRead = stream.Read(buffer, totalBytesRead, buffer.Length - totalBytesRead)) > 0)
                {
                    // 将接收到的字节转换为字符串  
                    responseData = Encoding.UTF8.GetString(buffer, 0, bytesRead);
                    Console.WriteLine("Received from server: " + responseData);
                    Console.WriteLine("ReceivedBytes:{0}", bytesRead);
                    totalBytesRead += bytesRead;
                    // 可以在这里处理已经读取的数据(例如,写入文件或进行其他处理)  
                    // 但请注意,如果处理逻辑复杂,最好先将数据复制到另一个缓冲区中  
                }

                // 现在 totalBytesRead 包含了从流中读取的总字节数
                Console.WriteLine("TotalReceivedBytes:{0}", totalBytesRead);
                //将Json写入文件
                File.WriteAllText("response.html", responseData);

                // 关闭连接  
                client.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e.Message);
            }
}
        static void Main(string[] args)
        {
            Console.WriteLine("请求Cploar动态域名和端口信息......");
            GetCploarInfo();
            string jsonContent=File.ReadAllText("cpolarinfo.json");
            Console.WriteLine(jsonContent);
            CpolarInfo cpinfo = JsonConvert.DeserializeObject<CpolarInfo>(jsonContent);
            Console.WriteLine($"{cpinfo.total}");
            for (int i = 0;i<cpinfo.total;i++)
            {
                Console.WriteLine($"{cpinfo.items[i].name}\r\n{cpinfo.items[i].create_datetime}\r\n{cpinfo.items[i].pubulic_url}");
            }
            string host = string.Empty;
            for (int i = 0; i < cpinfo.total; i++)
            {
                if (cpinfo.items[i].name== "GrassWebProxy")
                {
                    host = cpinfo.items[i].pubulic_url;
                }
            }
            Console.WriteLine(host);
            // 使用Uri类来解析URL(这是更推荐的方法,因为它更健壮且能处理各种URL格式)  
            Uri uri = new Uri(host);

            // 直接从Uri对象中获取主机名  
            string hostname = uri.Host;
            int port = uri.Port;
            Console.WriteLine($"{hostname}:{port}"); // 输出: cpolard.26.tcp.cpolar.top  
            UseWebProxy("https://www.speedtest.net/", hostname, port);
        }
    }
}

GrassWebProxyClientV3:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;

namespace GrassWebProxyV3
{
    public class IPport
    {
        public string IP { get; set; }
        public int Port { get; set; }
    }
    public class CpolarInfo
    {
        public int total { get; set; }
        public item[] items { get; set; }
    }
    public class item
    {
        public string name { get; set; }
        public string create_datetime { get; set; }
        public string pubulic_url { get; set; }
    }
    public class Ticket
    {
        public ulong TicketNo { get; set; }
        public string BeginDateTime { get; set; }
        public string EndDateTime { get; set; }
    }
    internal class Program
    {
        static string GetCploarInfo()
        {
            try
            {
                string jsonFilePath = "ipport.json"; // 替换为你的JSON文件路径  

                // 读取文件内容  
                string jsonContent = File.ReadAllText(jsonFilePath);

                // 反序列化JSON字符串到对象  
                IPport ipaddr = JsonConvert.DeserializeObject<IPport>(jsonContent);

                // 输出结果  
                Console.WriteLine($"{ipaddr.IP}:{ipaddr.Port}");

                // 创建一个TcpClient实例并连接到服务器  
                TcpClient client = new TcpClient($"{ipaddr.IP}", ipaddr.Port);

                // 获取一个NetworkStream对象以进行读写  
                NetworkStream stream = client.GetStream();

                // 将消息转换为字节数组  
                //string message = "Hello from the client!";
                //byte[] data = Encoding.ASCII.GetBytes(message);
                byte[] data = { 0x4E, 0x74, 0x01, 0xF2, 0x00, 0x00, 0x00, 0x01 };

                // 发送消息到服务器  
                stream.Write(data, 0, data.Length);

                // 读取服务器的响应  
                byte[] buffer = new byte[4096];
                int bytesRead = stream.Read(buffer, 0, buffer.Length);

                // 将接收到的字节转换为字符串  
                string responseData = Encoding.ASCII.GetString(buffer, 0, bytesRead);
                Console.WriteLine("Received from server: " + responseData);
                Console.WriteLine("ReceivedBytes:{0}", bytesRead);
                // 关闭连接  
                client.Close();
                //将Json写入文件
                File.WriteAllText("cpolarinfo.json", responseData);

                return responseData;
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e.Message);
            }
            return string.Empty;
        }
        static void UseWebProxy(string webProxyUrl, string ip, int port)
        {
            try
            {
                string jsonFilePath = "Ticket1.json";
                // 读取文件内容  
                string jsonContent = File.ReadAllText(jsonFilePath);
                // 反序列化JSON字符串到对象  
                Ticket ticket = JsonConvert.DeserializeObject<Ticket>(jsonContent);

                // 输出结果  
                Console.WriteLine($"{ticket.TicketNo}\r\n{ticket.BeginDateTime}---{ticket.EndDateTime}");
                //组装
                byte[] data = { 0x47, 0x50, 0x01, 0xF2, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01 };
                for (int i = 2, j = 7; i < data.Length; i++, j--)
                {
                    data[i] = (byte)(ticket.TicketNo >> (j * 8));
                    //Console.WriteLine(data[i]);
                }

                // 将消息转换为字节数组  
                // 将 data 和 requestdata 合并到 buffer 中  
                byte[] requestdata = Encoding.UTF8.GetBytes(webProxyUrl);
                byte[] buf = new byte[data.Length + requestdata.Length];
                Buffer.BlockCopy(data, 0, buf, 0, data.Length);
                Buffer.BlockCopy(requestdata, 0, buf, data.Length, requestdata.Length);
                
                // 创建一个TcpClient实例并连接到服务器  
                TcpClient client = new TcpClient(ip, port);

                // 获取一个NetworkStream对象以进行读写  
                NetworkStream stream = client.GetStream();
                // 发送消息到服务器
                stream.Write(buf, 0, buf.Length);

                // 读取服务器的响应  
                byte[] buffer = new byte[81920];
                //int bytesRead = stream.Read(buffer, 0, buffer.Length);
                int totalBytesRead = 0;
                int bytesRead = 0;
                string responseData = string.Empty;

                while ((bytesRead = stream.Read(buffer, totalBytesRead, buffer.Length - totalBytesRead)) > 0)
                {
                    // 将接收到的字节转换为字符串  
                    responseData = Encoding.UTF8.GetString(buffer, 0, bytesRead);
                    Console.WriteLine("Received from server: " + responseData);
                    Console.WriteLine("ReceivedBytes:{0}", bytesRead);
                    totalBytesRead += bytesRead;
                    // 可以在这里处理已经读取的数据(例如,写入文件或进行其他处理)  
                    // 但请注意,如果处理逻辑复杂,最好先将数据复制到另一个缓冲区中  
                }

                // 现在 totalBytesRead 包含了从流中读取的总字节数
                Console.WriteLine("TotalReceivedBytes:{0}", totalBytesRead);

                // 关闭连接  
                client.Close();

                //将Json写入文件
                File.WriteAllText("response.html", responseData);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e.Message);
            }
        }
        static void Main(string[] args)
        {
            Console.WriteLine("请求Cploar动态域名和端口信息......");
            string jsonContent = GetCploarInfo();
            CpolarInfo cpinfo = new CpolarInfo();
            if (jsonContent==string.Empty)
            {
                jsonContent = File.ReadAllText("cpolarinfo.json");
            }
            else
            {
                //
                Console.WriteLine(jsonContent);
                cpinfo = JsonConvert.DeserializeObject<CpolarInfo>(jsonContent);
                Console.WriteLine($"{cpinfo.total}");
                for (int i = 0; i < cpinfo.total; i++)
                {
                    Console.WriteLine($"{cpinfo.items[i].name}\r\n{cpinfo.items[i].create_datetime}\r\n{cpinfo.items[i].pubulic_url}");
                }
            }

            string host = string.Empty;
            for (int i = 0; i < cpinfo.total; i++)
            {
                if (cpinfo.items[i].name == "GrassWebProxy")
                {
                    host = cpinfo.items[i].pubulic_url;
                }
            }
            Console.WriteLine(host);
            // 使用Uri类来解析URL(这是更推荐的方法,因为它更健壮且能处理各种URL格式)  
            Uri uri = new Uri(host);

            // 直接从Uri对象中获取主机名  
            string hostname = uri.Host;
            int port = uri.Port;
            Console.WriteLine($"{hostname}:{port}"); // 输出: cpolard.26.tcp.cpolar.top  
            UseWebProxy("https://www.speedtest.net/", hostname, port);
            //UseWebProxy("https://29bf2803.r15.cpolar.top/html/Welcome.html", hostname, port);
        }
    }
}

GrassWebProxyClientV4:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Policy;
using System.Net;
using System.Net.Sockets;

namespace GrassWebProxyClientV4
{
    public class IPport
    {
        public string IP { get; set; }
        public int Port { get; set; }
    }
    public class CpolarInfo
    {
        public int total { get; set; }
        public item[] items { get; set; }
    }
    public class item
    {
        public string name { get; set; }
        public string create_datetime { get; set; }
        public string pubulic_url { get; set; }
    }
    public class Ticket
    {
        public ulong TicketNo { get; set; }
        public string BeginDateTime { get; set; }
        public string EndDateTime { get; set; }
    }
    public class GP
    {
        public static bool CheckFlag(ref byte[] msg)
        {
            //"G" 71 0x47
            //"P" 80 0x50
            if (msg[0] == 0x47 && msg[1] == 0x50)
            {
                Console.WriteLine("CheckFlag OK");
                return true;
            }
            return false;
        }
        static public Ticket ReadConfig(string cfg = "Ticket1.json")
        {
            // 读取文件内容  
            string jsonContent = File.ReadAllText(cfg);

            // 反序列化JSON字符串到对象  
            Ticket ticket = JsonConvert.DeserializeObject<Ticket>(jsonContent);
            // 输出结果  
            Console.WriteLine($"{ticket.TicketNo}\r\n{ticket.BeginDateTime}---{ticket.EndDateTime}");
            return ticket;
        }
        static public bool CheckTicket(ref byte[] msg, Ticket counterfoil)
        {
            /*ulong ticket = (ulong)((msg[2] << 56) | (msg[3] << 48) | (msg[4] << 40)
                | (msg[5] << 32) | (msg[6] << 24) | (msg[7] << 16) | (msg[8] << 8) | msg[9]);*/
            ulong ticket = BitConverter.ToUInt64(msg, 2); // 从索引2开始读取8个字节
            //Console.WriteLine(ticket.ToString("X"));
            if (counterfoil.TicketNo == ticket)
            {
                return true;
            }
            return false;
        }
        static public bool CheckFlagAdTicket(ref byte[] msg, Ticket counterfoil)
        {
            //"G" 71 0x47
            //"P" 80 0x50
            ulong ticket = BitConverter.ToUInt64(msg, 2); // 从索引2开始读取8个字节
            if (msg[0] == 0x47 && msg[1] == 0x50 && counterfoil.TicketNo == ticket)
            {
                return true;
            }
            return false;
        }
    }

    public class DynamicCploar
    {
        public static Uri GetGrassWebProxyHost(string notifyServer = "ipport.json")
        {
            try
            {
                string jsonFilePath = "ipport.json"; // 替换为你的JSON文件路径  

                // 读取文件内容  
                string jsonContent = File.ReadAllText(jsonFilePath);

                // 反序列化JSON字符串到对象  
                IPport ipaddr = JsonConvert.DeserializeObject<IPport>(jsonContent);

                // 输出结果  
                Console.WriteLine($"Notify Server: {ipaddr.IP}:{ipaddr.Port}");

                // 创建一个TcpClient实例并连接到服务器  
                TcpClient client = new TcpClient($"{ipaddr.IP}", ipaddr.Port);

                // 获取一个NetworkStream对象以进行读写  
                NetworkStream stream = client.GetStream();

                // 将消息转换为字节数组  
                //string message = "Hello from the client!";
                //byte[] data = Encoding.ASCII.GetBytes(message);
                byte[] data = { 0x4E, 0x74, 0x01, 0xF2, 0x00, 0x00, 0x00, 0x01 };

                // 发送消息到服务器  
                stream.Write(data, 0, data.Length);

                // 读取服务器的响应  
                byte[] buffer = new byte[1024];
                int bytesRead = stream.Read(buffer, 0, buffer.Length);

                // 将接收到的字节转换为字符串  
                string responseData = Encoding.ASCII.GetString(buffer, 0, bytesRead);
                //Console.WriteLine("Received from server: " + responseData);
                //Console.WriteLine("ReceivedBytes:{0}", bytesRead);
                // 关闭连接  
                client.Close();
                //将Json写入文件
                File.WriteAllText("cpolarinfo.json", responseData);

                CpolarInfo cpinfo = new CpolarInfo();
                if (responseData == string.Empty)
                {
                    responseData = File.ReadAllText("cpolarinfo.json");
                }
                else
                {
                    //
                    Console.WriteLine(responseData);
                    cpinfo = JsonConvert.DeserializeObject<CpolarInfo>(responseData);
                    //Console.WriteLine($"{cpinfo.total}");
                    //for (int i = 0; i < cpinfo.total; i++)
                    //{
                    //    Console.WriteLine($"{cpinfo.items[i].name}\r\n{cpinfo.items[i].create_datetime}\r\n{cpinfo.items[i].pubulic_url}");
                    //}
                }

                string host = string.Empty;
                for (int i = 0; i < cpinfo.total; i++)
                {
                    if (cpinfo.items[i].name == "GrassWebProxy")
                    {
                        host = cpinfo.items[i].pubulic_url;
                    }
                }
                Console.WriteLine(host);
                // 使用Uri类来解析URL(这是更推荐的方法,因为它更健壮且能处理各种URL格式)  
                Uri uri = new Uri(host);

                return uri;
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e.Message);
            }
            return null;
        }
    }
    internal class Program
    {
        public static async Task Main(string[] args)
        {
            // 代理服务器信息  
            var host = DynamicCploar.GetGrassWebProxyHost("ipport.json");
            string proxyAddress = host.Host;
            int proxyPort = host.Port; // 代理服务器的端口  
                                   // 创建HttpClientHandler并设置代理  
            var handler = new HttpClientHandler
            {
                Proxy = new WebProxy(proxyAddress, proxyPort),
                UseProxy = true, // 默认情况下UseProxy是true,但明确设置可以避免混淆  
                                 // 如果代理服务器需要认证,可以添加以下两行(替换username和password)  
                                 // Proxy = new WebProxy(proxyAddress, proxyPort)  
                                 // {  
                                 //     Credentials = new NetworkCredential("username", "password")  
                                 // },  
            };
            using (HttpClient client = new HttpClient(handler))
            {
                string url = "https://www.iciba.com/";
                //加Headers["X-Proxy-Ticket"]
                var ticket = GP.ReadConfig("Ticket1.json");
                //"G" 71 0x47
                //"P" 80 0x50
                byte[] tickBytes = new byte[10];
                tickBytes[0] = 0x47;
                tickBytes[1] = 0x50;
                Buffer.BlockCopy(BitConverter.GetBytes(ticket.TicketNo), 0, tickBytes, 2, 8);
                string ticketHeader = Encoding.UTF8.GetString(tickBytes);
                // 设置请求头  
                client.DefaultRequestHeaders.Add("X-Proxy-Ticket", ticketHeader);
                HttpResponseMessage response = await client.GetAsync(url);

                response.EnsureSuccessStatusCode(); // 抛出异常如果状态码表示错误  
                string responseBody = await response.Content.ReadAsStringAsync();
                Console.WriteLine(responseBody);
            }
        }
    }
}

GrassWebProxyClientV5:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.IO.Compression;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace GrassWebProxyClientV5
{
    public class IPport
    {
        public string IP { get; set; }
        public int Port { get; set; }
    }
    public class CpolarInfo
    {
        public int total { get; set; }
        public item[] items { get; set; }
    }
    public class item
    {
        public string name { get; set; }
        public string create_datetime { get; set; }
        public string pubulic_url { get; set; }
    }
    public class CustomDateTimeConverter : IsoDateTimeConverter
    {
        public CustomDateTimeConverter()
        {
            DateTimeFormat = "yyyyMMdd'T'HH:mm:ss";
        }
    }
    public class Ticket
    {
        public ulong TicketNo { get; set; }

        [JsonConverter(typeof(CustomDateTimeConverter))]
        public DateTime BeginDateTime { get; set; }

        [JsonConverter(typeof(CustomDateTimeConverter))]
        public DateTime EndDateTime { get; set; }
    }
    public class GP
    {
        public static bool CheckFlag(ref byte[] msg)
        {
            //"G" 71 0x47
            //"P" 80 0x50
            if (msg[0] == 0x47 && msg[1] == 0x50)
            {
                Console.WriteLine("CheckFlag OK");
                return true;
            }
            return false;
        }
        static public Ticket ReadConfig(string cfg = "Ticket1.json")
        {
            // 读取文件内容  
            string jsonContent = File.ReadAllText(cfg);

            // 反序列化JSON字符串到对象  
            Ticket ticket = JsonConvert.DeserializeObject<Ticket>(jsonContent);
            // 输出结果  
            Console.WriteLine($"{ticket.TicketNo}\r\n{ticket.BeginDateTime}---{ticket.EndDateTime}");
            return ticket;
        }
        static public bool CheckTicket(ref byte[] msg, Ticket counterfoil)
        {
            /*ulong ticket = (ulong)((msg[2] << 56) | (msg[3] << 48) | (msg[4] << 40)
                | (msg[5] << 32) | (msg[6] << 24) | (msg[7] << 16) | (msg[8] << 8) | msg[9]);*/
            ulong ticket = BitConverter.ToUInt64(msg, 2); // 从索引2开始读取8个字节
                                                          //Console.WriteLine(ticket.ToString("X"));
            if (counterfoil.TicketNo == ticket)
            {
                return true;
            }
            return false;
        }
        static public bool CheckFlagAdTicket(ref byte[] msg, Ticket counterfoil)
        {
            //"G" 71 0x47
            //"P" 80 0x50
            ulong ticket = BitConverter.ToUInt64(msg, 2); // 从索引2开始读取8个字节
            if (msg[0] == 0x47 && msg[1] == 0x50 && counterfoil.TicketNo == ticket)
            {
                return true;
            }
            return false;
        }

        public static byte[] Compress(byte[] input)
        {
            using (var memoryStream = new MemoryStream())
            {
                using (var gzipStream = new GZipStream(memoryStream, CompressionMode.Compress, true))
                {
                    gzipStream.Write(input, 0, input.Length);
                }

                // 确保所有数据都被写入并压缩  
                return memoryStream.ToArray();
            }
        }
        public static byte[] DecompressGzip(byte[] gzip, int prebytes = 4096)
        {
            using (var ms = new MemoryStream(gzip))
            {
                using (var gzipStream = new GZipStream(ms, CompressionMode.Decompress))
                {
                    var buffer = new byte[prebytes];
                    using (var memoryStreamOut = new MemoryStream())
                    {
                        int read;
                        while ((read = gzipStream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            memoryStreamOut.Write(buffer, 0, read);
                        }
                        return memoryStreamOut.ToArray();
                    }
                }
            }
        }
    }

    public class DynamicCploar
    {
        public static Uri GetGrassWebProxyHost(string notifyServer = "ipport.json")
        {
            try
            {
                // 读取文件内容  
                string jsonContent = File.ReadAllText(notifyServer);

                // 反序列化JSON字符串到对象  
                IPport ipaddr = JsonConvert.DeserializeObject<IPport>(jsonContent);

                // 输出结果  
                Console.WriteLine($"Notify Server: {ipaddr.IP}:{ipaddr.Port}");

                // 创建一个TcpClient实例并连接到服务器  
                TcpClient client = new TcpClient($"{ipaddr.IP}", ipaddr.Port);

                // 获取一个NetworkStream对象以进行读写  
                NetworkStream stream = client.GetStream();

                // 将消息转换为字节数组  
                //string message = "Hello from the client!";
                //byte[] data = Encoding.ASCII.GetBytes(message);
                byte[] data = { 0x4E, 0x74, 0x01, 0xF2, 0x00, 0x00, 0x00, 0x01 };

                // 发送消息到服务器  
                stream.Write(data, 0, data.Length);

                // 读取服务器的响应  
                byte[] buffer = new byte[1024];
                int bytesRead = stream.Read(buffer, 0, buffer.Length);

                // 将接收到的字节转换为字符串  
                string responseData = Encoding.ASCII.GetString(buffer, 0, bytesRead);
                //Console.WriteLine("Received from server: " + responseData);
                //Console.WriteLine("ReceivedBytes:{0}", bytesRead);
                // 关闭连接  
                client.Close();
                //将Json写入文件
                File.WriteAllText("cpolarinfo.json", responseData);

                CpolarInfo cpinfo = new CpolarInfo();
                if (responseData == string.Empty)
                {
                    responseData = File.ReadAllText("cpolarinfo.json");
                }
                else
                {
                    //
                    Console.WriteLine(responseData);
                    cpinfo = JsonConvert.DeserializeObject<CpolarInfo>(responseData);
                    //Console.WriteLine($"{cpinfo.total}");
                    //for (int i = 0; i < cpinfo.total; i++)
                    //{
                    //    Console.WriteLine($"{cpinfo.items[i].name}\r\n{cpinfo.items[i].create_datetime}\r\n{cpinfo.items[i].pubulic_url}");
                    //}
                }

                string host = string.Empty;
                for (int i = 0; i < cpinfo.total; i++)
                {
                    if (cpinfo.items[i].name == "GrassWebProxy")
                    {
                        host = cpinfo.items[i].pubulic_url;
                    }
                }
                Console.WriteLine(host);
                // 使用Uri类来解析URL(这是更推荐的方法,因为它更健壮且能处理各种URL格式)  
                Uri uri = new Uri(host);

                return uri;
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e.Message);
            }
            return null;
        }
    }
    internal class Program
    {
        static void UseWebProxy(string webProxyUrl, string ip, int port)
        {
            try
            {
                string jsonFilePath = "Ticket1.json";
                // 读取文件内容  
                string jsonContent = File.ReadAllText(jsonFilePath);
                // 反序列化JSON字符串到对象  
                Ticket ticket = JsonConvert.DeserializeObject<Ticket>(jsonContent);

                // 输出结果  
                Console.WriteLine($"{ticket.TicketNo},{ticket.BeginDateTime.ToString("yyyyMMdd'T'HH:mm:ss")}---{ticket.EndDateTime.ToString("yyyyMMdd'T'HH:mm:ss")}");
                //组装
                byte[] data = { 0x47, 0x50, 0x01, 0xF2, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01 };
                for (int i = 2, j = 7; i < data.Length; i++, j--)
                {
                    data[i] = (byte)(ticket.TicketNo >> (j * 8));
                    //Console.WriteLine(data[i]);
                }

                // 将消息转换为字节数组  
                // 将 data 和 requestdata 合并到 buffer 中  
                byte[] requestdata = Encoding.UTF8.GetBytes(webProxyUrl);
                byte[] buf = new byte[data.Length + requestdata.Length];
                Buffer.BlockCopy(data, 0, buf, 0, data.Length);
                Buffer.BlockCopy(requestdata, 0, buf, data.Length, requestdata.Length);

                // 创建一个TcpClient实例并连接到服务器  
                TcpClient client = new TcpClient(ip, port);

                // 获取一个NetworkStream对象以进行读写  
                NetworkStream stream = client.GetStream();
                // 发送消息到服务器
                stream.Write(buf, 0, buf.Length);

                // 读取服务器的响应  
                byte[] buffer = new byte[81920];
                int totalBytesRead = 0;
                int bytesRead = 0;
                string responseData = string.Empty;

                while ((bytesRead = stream.Read(buffer, totalBytesRead, buffer.Length - totalBytesRead)) > 0)
                {
                    Console.WriteLine("ReceivedBytes:{0}", bytesRead);
                    totalBytesRead += bytesRead;
                }
                // 关闭连接  
                client.Close();

                //解压
                var tmp = GP.DecompressGzip(buffer,totalBytesRead);
                // 将接收到的字节转换为字符串  
                responseData = Encoding.UTF8.GetString(tmp, 0, tmp.Length);

                // 现在 totalBytesRead 包含了从流中读取的总字节数
                Console.WriteLine("TotalReceivedBytes:{0}", totalBytesRead);
                Console.WriteLine("Received from server:\r\n" + responseData);


                //将Json写入文件
                File.WriteAllText("response.html", responseData);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e.Message);
            }
        }
        static void Main(string[] args)
        {
            // 代理服务器信息  
            var host = DynamicCploar.GetGrassWebProxyHost("ipport.json");
            //string proxyAddress = host.Host;
            //int proxyPort = host.Port; // 代理服务器的端口  
                                       // 创建HttpClientHandler并设置代理  

            UseWebProxy("https://www.baidu.com/",host.Host,host.Port);
            string url="https://access-hsk.oray.com/loading?r=https%253A%252F%252Fu1506257x0%252Egoho%252Eco%253A443%252Fhtml%252Fopendata%252Ehtml&i=aHR0cHM6Ly91MTUwNjI1N3gwLmdvaG8uY286NDQzLDE0LjE1MS44MS43Mg%253D%253D&p=2979654771&k=aHV4eWNjXzE0LjE1MS44MS43Ml9FZGdlXzEyNyUyRTAlMkUwJTJFMF9XaW5kb3dzX1dpbmRvd3MlMjAxMA%3D%3D";
            UseWebProxy(url, host.Host, host.Port);
            url = "http://www.cjors.cn/";
            UseWebProxy(url, host.Host, host.Port);
        }
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/966075.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

DeepSeek 引领的 AI 范式转变与存储架构的演进

近一段时间&#xff0c;生成式 AI 技术经历了飞速的进步&#xff0c;尤其是在强推理模型&#xff08;Reasoning-LLM&#xff09;的推动下&#xff0c;AI 从大模型训练到推理应用的范式发生了剧变。以 DeepSeek 等前沿 AI 模型为例&#xff0c;如今的 AI 技术发展已不局限于依赖…

vscode 设置在编辑器的标签页超出可视范围时自动换行(workbench.editor.wrapTabs)

“workbench.editor.wrapTabs”: true 是 VS Code&#xff08;Visual Studio Code&#xff09; 的一个设置项&#xff0c;它的作用是 在编辑器的标签页超出可视范围时自动换行&#xff0c;而不是显示滚动条。 需要修改settings.json 参考&#xff1a;settings.json 默认值&a…

高端入门:Ollama 本地高效部署DeepSeek模型深度搜索解决方案

目录 一、Ollama 介绍 二、Ollama下载 2.1 官网下载 2.2 GitHub下载 三、模型库 四、Ollmal 使用 4.1 模型运行&#xff08;下载&#xff09; 4.2 模型提问 五、Ollama 常用命令 相关推荐 一、Ollama 介绍 Ollama是一个专为在本地机器上便捷部署和运行大型语言模型&…

前端组件标准化专家Prompt指令的最佳实践

前端组件标准化专家Prompt 提示词可作为项目自定义提示词使用&#xff0c;本次提示词偏向前端开发的使用&#xff0c;如有需要可适当修改关键词和示例 推荐使用 Cursor 中作为自定义指令使用Cline 插件中作为自定义指令使用在力所能及的范围内使用最好的模型&#xff0c;可以…

介绍10个比较优秀好用的Qt相关的开源库

记录下比较好用的一些开源库 1. Qt中的日志库“log4qt” log4qt 是一个基于 Apache Log4j 设计理念的 Qt 日志记录库&#xff0c;它为 Qt 应用程序提供了强大而灵活的日志记录功能。Log4j 是 Java 领域广泛使用的日志框架&#xff0c;log4qt 借鉴了其优秀的设计思想&#xff…

如何打造一个更友好的网站结构?

在SEO优化中&#xff0c;网站的结构往往被忽略&#xff0c;但它其实是决定谷歌爬虫抓取效率的关键因素之一。一个清晰、逻辑合理的网站结构&#xff0c;不仅能让用户更方便地找到他们需要的信息&#xff0c;还能提升搜索引擎的抓取效率 理想的网站结构应该像一棵树&#xff0c;…

态、势、感、知中的信息

“态、势中的信息”与“感、知中的信息”分别对应客观系统状态与主观认知过程的信息类型&#xff0c;其差异体现在信息的来源、性质、处理方式及作用目标上。以下通过对比框架和具体案例解析两者的区别&#xff1a; 态势中的信息中的态信息指系统在某一时刻的客观存在状态&…

文本生图的提示词prompt和参数如何设置(基于Animagine XL V3.1)

昨天搞了半天 Animagine XL V3.1&#xff0c;发现市面上很多教程只是授之以鱼&#xff0c;并没有授之以渔的。也是&#xff0c;拿来赚钱不好吗&#xff0c;闲鱼上部署一个 Deepseek 都能要两百块。这里我还是想写篇文章介绍一下&#xff0c;虽不全面&#xff0c;但是尽量告诉你…

基于docker搭建Kafka集群,使用内部自带的Zookeeper方式搭建

前提条件 按照【kafka3.8.0升级文档成功搭建kafka服务】 环境&#xff1a;192.168.2.91 192.168.2.93 并以192.168.2.91环境kafka自带的zookeeper作为协调器。 使用基于KRaft方式进行kafka集群搭建教程 搭建kafka-ui可视化工具 1、创建kafka集群节点192.168.2.91 &#xff…

GitPuk快速安装配置教程(入门级)

GitPuk是一款国产开源免费的代码管理工具&#xff0c;工具简洁易用&#xff0c;开源免费&#xff0c;本文将讲解如何快速安装和配置GitPuk&#xff0c;以快速入门上手。 1、安装 支持 Windows、Mac、Linux、docker 等操作系统。 1.1 Linux安装&#xfeff; 以下以Centos7安装…

奖励模型中的尺度扩展定律和奖励劫持

奖励模型中的尺度扩展定律和奖励劫持 FesianXu 20250131 at Wechat Search Team 前言 最近在考古一些LLM的经典老论文&#xff0c;其中有一篇是OpenAI于ICML 2023年发表的文章&#xff0c;讨论了在奖励模型&#xff08;Reward Model&#xff09;中的尺度扩展规律&#xff08;S…

ASP.NET Core中Filter与Middleware的区别

中间件是ASP.NET Core这个基础提供的功能&#xff0c;而Filter是ASP.NET Core MVC中提供的功能。ASP.NET Core MVC是由MVC中间件提供的框架&#xff0c;而Filter属于MVC中间件提供的功能。 区别 中间件可以处理所有的请求&#xff0c;而Filter只能处理对控制器的请求&#x…

力扣240 搜索二维矩阵 ll

编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target 。该矩阵具有以下特性&#xff1a; 每行的元素从左到右升序排列。每列的元素从上到下升序排列。 示例 1&#xff1a; 输入&#xff1a;matrix [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,…

Redis03 - 高可用

Redis高可用 文章目录 Redis高可用一&#xff1a;主从复制 & 读写分离1&#xff1a;主从复制的作用2&#xff1a;主从复制原理2.1&#xff1a;全量复制2.2&#xff1a;增量复制&#xff08;环形缓冲区&#xff09; 3&#xff1a;主从复制实际演示3.1&#xff1a;基本流程准…

JAVA安全—FastJson反序列化利用链跟踪autoType绕过

前言 FastJson这个漏洞我们之前讲过了,今天主要是对它的链条进行分析一下,明白链条的构造原理。 Java安全—log4j日志&FastJson序列化&JNDI注入_log4j漏洞-CSDN博客 漏洞版本 1.2.24及以下没有对序列化的类做校验,导致漏洞产生 1.2.25-1.2.41增加了黑名单限制,…

vmware ubuntu 扩展硬盘系统文件大小

首先&#xff0c;在VMware中添加扩展硬盘大小&#xff1a; 通过lsblk指令&#xff0c;可以看到添加的未分配硬盘大小情况&#xff1a; NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS fd0 2:0 1 4K 0 disk loop0 7:0 0 4K 1 loop /snap/bare/5 loop1…

DeepSeek R1 Distill Llama 70B(免费版)API使用详解

DeepSeek R1 Distill Llama 70B&#xff08;免费版&#xff09;API使用详解 在人工智能领域&#xff0c;随着技术的不断进步&#xff0c;各种新的模型和应用如雨后春笋般涌现。今天&#xff0c;我们要为大家介绍的是OpenRouter平台上提供的DeepSeek R1 Distill Llama 70B&…

阿里云 | DeepSeek人工智能大模型安装部署

ModelScope是阿里云人工智能大模型开源社区 ModelScope网络链接地址 https://www.modelscope.cn DeepSeek模型库网络链接地址 https://www.modelscope.cn/organization/deepseek-ai 如上所示&#xff0c;在阿里云人工智能大模型开源社区ModelScope中&#xff0c;使用阿里云…

kafka服务端之控制器

文章目录 概述控制器的选举与故障恢复控制器的选举故障恢复 优雅关闭分区leader的选举 概述 在Kafka集群中会有一个或多个broker&#xff0c;其中有一个broker会被选举为控制器&#xff08;Kafka Controler&#xff09;&#xff0c;它负责管理整个集群中所有分区和副本的状态。…

03/29 使用 海康SDK 对接时使用的 MysqlUtils

前言 最近朋友的需求, 是需要使用 海康sdk 连接海康设备, 进行数据的获取, 比如 进出车辆, 进出人员 这一部分是 资源比较贫瘠时的一个 Mysql 工具类 测试用例 public class MysqlUtils {public static String MYSQL_HOST "192.168.31.9";public static int MY…