zoukankan      html  css  js  c++  java
  • 一个服务器上面配置多个IP ,实现指定IP的域名请求

    //配置多个IP命名
    using System.Net;

    //***************************************************************************

    /// <summary>
            /// 通过设置这个属性,可以在发出连接的时候绑定客户端发出连接所使用的IP地址。 
            /// </summary>
            /// <param name="servicePoint"></param>
            /// <param name="remoteEndPoint"></param>
            /// <param name="retryCount"></param>
            /// <returns></returns>
            public static IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount)
            {
                return new IPEndPoint(IPAddress.Parse("192.168.1.1") , 0);//端口号
            }
            /// <summary>
            /// 一个服务器上面配置多个IP 固定出网IP
            /// </summary>
            public static void MakeRequest() 
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.baidu.com");
                //设置本地的出口ip和端口
                request.ServicePoint.BindIPEndPointDelegate = new BindIPEndPoint(BindIPEndPointCallback);
                if (ServicePointManager.DefaultConnectionLimit < 10)
                {
                    ServicePointManager.DefaultConnectionLimit = 10;
                }
                //req.ServicePoint.ConnectionLimit=int.Max;  //允许最大连接数 
    
                HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
                response.ToString(); 
            }
    

     HttpWebRequest的详细方法二:

    System.Net.HttpWebRequest myRequest = null;
                System.Net.HttpWebResponse myResponse = null;
                Stream reqStream = null;
                Stream resStream = null;
                string signkey = "", url = "";
                string sdateStr = DateTime.Now.AddDays(-6).ToString("yyyyMMdd") + "000001"; 
                try
                {
                    signkey = XH_ChannelKey;//key
                    url = XH_ChannelUrl;    //接口地址
                    url += "fromDate=" + sdateStr;
                    url += "&version=1.4";//版本号
                    url += "&hmac=" + MD5Encrypt(signkey + sdateStr + "1.4");
                    //想服务器端发送请求,获取订单信息      
                    myRequest = System.Net.WebRequest.Create(url) as System.Net.HttpWebRequest;
                    //--------------------- c#中HttpWebRequest使用Proxy实现指定IP的域名请求 ---------------------------------
                    //需要使用Proxy和其配置 (代理ip)                                  
                    ////System.Net.WebProxy proxy = new System.Net.WebProxy("115.238.128.138", 80);
                    ////myRequest.Proxy = proxy;
                    //------------------------------------------------------
                    myRequest.Timeout = 1000 * 60 * 1;//1分钟超时  1 minutes timeout
                    myRequest.Method = "POST";
                    myResponse = myRequest.GetResponse() as System.Net.HttpWebResponse;
                    Stream myResponseStream = myResponse.GetResponseStream();
                    StreamReader myStreamReader;
                    myStreamReader = new StreamReader(myResponseStream, System.Text.Encoding.GetEncoding("utf-8"));
                    //post返回的数据
                    string receiveData = myStreamReader.ReadToEnd();
                    myStreamReader.Close();
                    myResponseStream.Close();
    
                }
                catch (Exception ex)
                {
                    string stacktrace = ex.StackTrace;//获得详细的错误位置
                    string errpoint = stacktrace.Substring(stacktrace.IndexOf("位置"), stacktrace.Length - stacktrace.IndexOf("位置"));
    
                    Common.WriteTextLog("Error","", ex.Message + Environment.NewLine + errpoint);
                }
                finally
                {
                    if (resStream != null)
                    {
                        resStream.Close();
                    }
                    if (reqStream != null)
                    {
                        reqStream.Close();
                    }
                    if (myResponse != null)
                    {
                        myResponse.Close();
                    }
                    if (myRequest != null)
                    {
                        myRequest.Abort();
                    }
                    ////特别留意这句Sleep的调用!!
                    System.Threading.Thread.Sleep(16);
                }
    
  • 相关阅读:
    【转帖】分享一个迅为4412开发板OTG烧录批处理文件
    4412开发板图像识别项目-移植百度AI依赖库curl(二)
    4412开发板图像识别项目-初识人工智能(一)
    迅为4412开发板门禁系统项目的硬件框架扩展
    Linux开发板
    迅为i.MX6Q开发板用于中3D打印设备
    迅为I.MX6ULL开发板移植Linux5.4内核教程
    嵌入式开发与学习——迅为IMX6ULL开源硬件开发板
    迅为4412开发板实战机车导航-GPS定位系统
    迅为IMX6ULL开发板可外接模块丰富,兼容性强
  • 原文地址:https://www.cnblogs.com/Byrd/p/4353400.html
Copyright © 2011-2022 走看看