注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

一路奔跑

奔跑着一路向前...

 
 
 

日志

 
 

实现多个IP ,提交请求  

2014-05-11 15:15:32|  分类: C# |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

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

  评论这张
 
阅读(82)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017