C# Socket5代理客户端链接 LH Socket5 Server源码

本文介绍了一个使用LHSocket5Server代理服务访问特定网站(如土豆网)的具体实现方法。通过C#代码示例展示了如何建立代理连接、发送HTTP请求头并接收响应数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



使用方法

        private void Button1_Click(object sender, EventArgs e)
        {

            // 下面以来接代理访问土豆网为例可直接使用IP地址或域名方式进行代理

            {
                System.Net.Sockets.Socket VSocket = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);

                int Result = SetConnection(ref VSocket, "192.168.1.6", 60000, "U602414", "P257638", 1, "123.126.98.146", 80);
               // int Result = SetConnection(ref VSocket, "192.168.1.6", 60000, "U602414", "P257638", 2, "www.tudou.com", 80);
                if (Result != 0)
                {
                    TextBox1.AppendText("LhS5服务器连接信息设置失败! 请查看错误代码的对应信息! 错误代码:" + Result.ToString());
                    // -1 本地连接到代理服务器或发送数据到代理服务器失败 请检查本机连接是否正常
                    // 0=成功
                    // 1=身份失败
                    // 2=服务器连接用户要代理的目标地址失败
                    // 3=服务器解析用户发送的域名地址失败
                    // 255=失败
                    return;
                }


                //发送头部
                System.Text.StringBuilder Content = new System.Text.StringBuilder();
                Content.Append("GET /  HTTP/1.0\r\n");
                Content.Append("User-Agent: Mozilla/5.0 Chrome/48.0.2564.109 Safari/537.36\r\n");
                Content.Append("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\n");
                Content.Append("Host: www.tudou.com\r\n\r\n");
                VSocket.Send(System.Text.Encoding.Default.GetBytes(Content.ToString()));


                System.Threading.Thread.Sleep(1000);
                //返回访问网址的信息  如果读取内容不全 、可使用返回的 Content-Length中的数量进行读取
                byte[] Vbyte = new byte[20001];
                int VIndex = VSocket.Receive(Vbyte);
                TextBox1.AppendText(System.Text.Encoding.UTF8.GetString(Vbyte) + "\n");


                VSocket.Close();
            }
        }

操作方法

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ClientA"></param>
        /// <param name="LhS5Ip">LH代理服务器程序下开通的用户账号的IP地址</param>
        /// <param name="LhS5Port">LH代理服务器程序下开通的用户账号的端口</param>
        /// <param name="LhS5UserName">LH代理服务器程序下开通的用户账号的账号</param>
        /// <param name="LhS5UserPassword">LH代理服务器程序下开通的用户账号的密码</param>
        /// <param name="ConnType_">设置要代理的访问的目标类型 1=IP4  2=域名  此访问类型是告诉lh服务器要代理连接到的目标地址</param>
        /// <param name="TragetAddress">根据上方输入内容  IP4地址 或  域名</param>
        /// <param name="TragetProt">上方配套的连接地址对应的端口</param>
        /// <returns></returns>
        private int SetConnection(ref System.Net.Sockets.Socket ClientA, string LhS5Ip, ushort LhS5Port, string LhS5UserName, string LhS5UserPassword, int ConnType_, string TragetAddress, ushort TragetProt)
        {


            // 蓝恒网络Socket5客户端连接演示
            // Http://www.lanheng.net
            // 下面代码开发者可根据自行情况摘取整合进自己的程序内进行相关修改 也可以直接使用



            int Result = 0;

            try
            {
                ClientA.Connect(System.Net.IPAddress.Parse(LhS5Ip), LhS5Port);
            }
            catch (Exception ex)
            {
                Console.WriteLine("错误信息:" + ex.Message);
                return -1;
            }

            int Vindex = 0;
            int Size = 6 + System.Text.Encoding.ASCII.GetBytes(LhS5UserName + LhS5UserPassword).Count();
            if (ConnType_ == 2)
            {
                Size += 1 + Encoding.ASCII.GetBytes(TragetAddress).Count();
            }
            else
            {
                Size += 4;
            }

            byte[] Vbyte = new byte[Size];
            Vbyte[0] = 85;
            Vbyte[1] = (byte)System.Text.Encoding.ASCII.GetBytes(LhS5UserName).Count();
            Vindex = 2;
            foreach (byte i in System.Text.Encoding.ASCII.GetBytes(LhS5UserName))
            {
                Vbyte[Vindex] = i;
                Vindex += 1;
            };

            Vbyte[Vindex] = (byte)System.Text.Encoding.ASCII.GetBytes(LhS5UserPassword).Count();
            Vindex += 1;
            foreach (byte i in System.Text.Encoding.ASCII.GetBytes(LhS5UserPassword))
            {
                Vbyte[Vindex] = i;
                Vindex += 1;
            }

            switch (ConnType_)
            {
                case 1:
                    Vbyte[Vindex] = 1;
                    Vindex += 1;
                    foreach (byte i in System.Net.IPAddress.Parse(TragetAddress).GetAddressBytes())
                    {
                        Vbyte[Vindex] = i;
                        Vindex += 1;
                    }

                    break;
                case 2:
                    Vbyte[Vindex] = 2;
                    Vindex += 1;
                    Vbyte[Vindex] = (byte)System.Text.Encoding.ASCII.GetBytes(TragetAddress).Count();
                    Vindex += 1;
                    foreach (byte i in System.Text.Encoding.ASCII.GetBytes(TragetAddress))
                    {
                        Vbyte[Vindex] = i;
                        Vindex += 1;
                    }

                    break;
            }




            foreach (byte i in BitConverter.GetBytes(BitConverter.ToUInt16(BitConverter.GetBytes(System.Net.IPAddress.HostToNetworkOrder(TragetProt)), 2)))
            {
                Vbyte[Vindex] = i;
                Vindex += 1;
            }
            ClientA.Send(Vbyte);

            Vbyte = new byte[2];
            Vindex = ClientA.Receive(Vbyte);
            if (Vindex < 2)
            {
                Console.WriteLine("获取服务器返回数据失败");
                ClientA.Close();
                return -1;
            }
            else
            {
                return Vbyte[1];
            }

        }

注意必须是链接  LH Socket5 Server 搭建的服务器才可以正常使用

帮助信息:http://www.lanheng.net/ProductContent/39/85

源码下载:http://down.lanheng.net/Tool/LhSocket5Server/Code/C.rar

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值