ServicePoint.BindIPEndPointDelegate freeze http request

  • Thread starter Thread starter Radzhab
  • Start date Start date
R

Radzhab

Guest
I have 2 IP addresses on my server. By default, first interface's ip uses in every application. But i want bind my c# program to another ip. I use code from this thread Choose one of many Internet connections for an application . All works correctly, but one moment. My simple http request by default ip is faster more than 50ms than binded. Why so? I suspect it because i use BindIPEndPointDelegate, but i dont understand why.

UseIP ip = new UseIP("10.8.17.245");

HttpWebRequest req = ip.CreateWebRequest(new Uri("https://api.binance.com/api/v1/ticker/24hr?symbol=BTCUSDT"));
using(WebResponse response = req.GetResponse()) {
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
}

public class UseIP {
public string IP {
get;
private set;
}

public UseIP(string IP) {
this.IP = IP;
}

public HttpWebRequest CreateWebRequest(Uri uri) {
ServicePoint servicePoint = ServicePointManager.FindServicePoint(uri);
servicePoint.BindIPEndPointDelegate = (servicePoint1, remoteEndPoint, retryCount) => {
IPAddress address = IPAddress.Parse(this.IP);
return new IPEndPoint(address, 0);
};

//Will cause bind to be called periodically
servicePoint.ConnectionLeaseTimeout = 0;

HttpWebRequest req = (HttpWebRequest) WebRequest.Create(uri);
//will cause bind to be called for each request (as long as the consumer of the request doesn't set it back to true!
req.KeepAlive = false;

return req;
}

Continue reading...
 
Back
Top