明天的明天 永远的永远 未知的一切 我与你一起承担 ??

是非成败转头空 青山依旧在 几度夕阳红 。。。
  博客园  :: 首页  :: 管理

//配置多个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);
            }