一个服务器上面配置多个IP ,实现指定IP的域名请求

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一个服务器上面配置多个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);
            }             
技术分享

 


以上是关于一个服务器上面配置多个IP ,实现指定IP的域名请求的主要内容,如果未能解决你的问题,请参考以下文章

怎么让域名解析到指定的端口

nginx怎么配置IP和域名都能访问

Apache虚拟目录实现同一个IP绑定多个域名

iis如何通过一个外网IP端口把内网多个站点发布到外网

一台服务器一个IP 如何绑定两个域名?

linux服务器上,其他机器通过curl+IP能够访问,但curl+域名访问超时