public static void FindServicePoint_PropertiesRoundtrip() { RemoteExecutor.Invoke(() => { string address = "http://" + Guid.NewGuid().ToString("N"); BindIPEndPoint expectedBindIPEndPointDelegate = delegate { return(null); }; int expectedConnectionLeaseTimeout = 42; int expectedConnectionLimit = 84; bool expected100Continue = false; int expectedMaxIdleTime = 200000; int expectedReceiveBufferSize = 123; bool expectedUseNagleAlgorithm = false; ServicePoint sp1 = ServicePointManager.FindServicePoint(address, null); sp1.BindIPEndPointDelegate = expectedBindIPEndPointDelegate; sp1.ConnectionLeaseTimeout = expectedConnectionLeaseTimeout; sp1.ConnectionLimit = expectedConnectionLimit; sp1.Expect100Continue = expected100Continue; sp1.MaxIdleTime = expectedMaxIdleTime; sp1.ReceiveBufferSize = expectedReceiveBufferSize; sp1.UseNagleAlgorithm = expectedUseNagleAlgorithm; ServicePoint sp2 = ServicePointManager.FindServicePoint(address, null); Assert.Same(expectedBindIPEndPointDelegate, sp2.BindIPEndPointDelegate); Assert.Equal(expectedConnectionLeaseTimeout, sp2.ConnectionLeaseTimeout); Assert.Equal(expectedConnectionLimit, sp2.ConnectionLimit); Assert.Equal(expected100Continue, sp2.Expect100Continue); Assert.Equal(expectedMaxIdleTime, sp2.MaxIdleTime); Assert.Equal(expectedReceiveBufferSize, sp2.ReceiveBufferSize); Assert.Equal(expectedUseNagleAlgorithm, sp2.UseNagleAlgorithm); }).Dispose(); }
/// <summary> /// Extends BeginInvoke so that when a state object is not needed, null does not need to be passed. /// <example> /// bindipendpoint.BeginInvoke(servicePoint, remoteEndPoint, retryCount, callback); /// </example> /// </summary> public static IAsyncResult BeginInvoke(this BindIPEndPoint bindipendpoint, ServicePoint servicePoint, IPEndPoint remoteEndPoint, Int32 retryCount, AsyncCallback callback) { if (bindipendpoint == null) { throw new ArgumentNullException("bindipendpoint"); } return(bindipendpoint.BeginInvoke(servicePoint, remoteEndPoint, retryCount, callback, null)); }
public RebindingHandler(IEnumerable <IPAddress> adapterAddresses, HttpMessageHandler innerHandler = null) : base(innerHandler ?? new WebRequestHandler()) { var addresses = adapterAddresses.ToList(); if (!addresses.Any()) { throw new ArgumentException(); } var idx = 0; bindHandler = (servicePoint, remoteEndPoint, retryCount) => { var index = (idx++) % addresses.Count; IPAddress adapterIpAddress = addresses[index]; return(new IPEndPoint(adapterIpAddress, 0)); }; }
public RebindingHandler(IEnumerable <IPAddress> adapterAddresses, HttpMessageHandler innerHandler = null) : base(innerHandler ?? new WebRequestHandler()) { var addresses = adapterAddresses.ToList(); if (!addresses.Any()) { throw new ArgumentException(); } var idx = 0; bindHandler = (servicePoint, remoteEndPoint, retryCount) => { int i = Interlocked.Increment(ref idx); uint i2 = unchecked ((uint)i); int index = (int)(((long)i2) % addresses.Count); IPAddress adapterIpAddress = addresses[index]; return(new IPEndPoint(adapterIpAddress, 0)); }; }
/// <summary> /// sets the ip endpoint to use for the connections estabilished by this HttpClientHandler /// </summary> /// <param name="handler"></param> /// <param name="biep"></param> public static void SetServicePointOptions(this HttpClientHandler handler, BindIPEndPoint biep) { if (handler == null) { throw new ArgumentNullException("handler cannot be null"); } var field = handler.GetType().GetField("_startRequest", BindingFlags.NonPublic | BindingFlags.Instance); // Fieldname has a _ due to being private if (field == null) { throw new ArgumentNullException($"Field _startRequest not found in handler type {handler.GetType()}"); } var startRequest = field.GetValue(handler) as Action <object>; if (startRequest == null) { throw new ArgumentNullException("startRequest value is null"); } Action <object> newStartRequest = obj => { var webReqField = obj.GetType().GetField("webRequest", BindingFlags.NonPublic | BindingFlags.Instance); if (webReqField == null) { throw new ArgumentNullException($"webRequest is not set on type {obj.GetType()}"); } var webRequest = webReqField.GetValue(obj) as HttpWebRequest; if (webRequest == null) { throw new ArgumentNullException($"webRequest is null"); } webRequest.ServicePoint.BindIPEndPointDelegate = new BindIPEndPoint(biep); startRequest(obj); //call original action }; field.SetValue(handler, newStartRequest); //replace original 'startRequest' with the one above }
/// <summary> /// 同步http的请求 /// </summary> /// <param name="url">地址</param> /// <param name="method">http方式</param> /// <param name="val">追加的值(post)</param> /// <param name="headers">头消息</param> /// <param name="encoding">编码</param> /// <param name="timeout">超时时间</param> /// <param name="point">端口协议</param> /// <returns></returns> public static HttpWebResponse SyncHttp(string url, string method, string val, NameValueCollection headers, Encoding encoding, int timeout, BindIPEndPoint point) { HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url); req.Method = method; req.ServicePoint.Expect100Continue = false; req.ServicePoint.BindIPEndPointDelegate = point; for (int i = 0; i < headers.AllKeys.Length; i++) { SetHeaderValue(req.Headers, headers.AllKeys[i], headers[headers.AllKeys[i]]); } if (string.IsNullOrEmpty(val) == false) { byte[] byteArray = encoding.GetBytes(val); req.ContentLength = byteArray.Length; Stream stream = req.GetRequestStream(); stream.Write(byteArray, 0, byteArray.Length); stream.Close(); } req.Timeout = timeout; HttpWebResponse response = (HttpWebResponse)req.GetResponse(); return(response); }