一個服務器上面配置多個IP ,實現指定IP的域名請求

//配置多個IP命名
using System.Net;c#

 

//***************************************************************************服務器

 /// <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的詳細方法二:post

 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);
            }             
相關文章
相關標籤/搜索