1

It appears that this can be achieved by setting up a callback function to the ServicePoint.

The solution (although the goal is different, the same technique can be used) is discussed in this question Specify the local endpoint for HTTP request.

Following it, I tried

var servicePoint = ServicePointManager.FindServicePoint(new Uri(uri));
servicePoint.BindIPEndPointDelegate = (servicePoint, remoteEndPoint, retryCount) =>
{
  if (remoteEndPoint.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
  {
    return new IPEndPoint(IPAddress.Any, 0);
  }

  throw new InvalidOperationException("no IPv4 address");
};

var _client = new HttpClient();
HttpResponseMessage response = await _client.GetAsync(uri);

expecting that the callback is called at the GetAsync() but it is not.

It would be much appreciated if someone tells me what I am missing. Or, a completely different approach to achieve my goal, to force IPv4 at connections that HttpClient makes, would also be very much welcome.

Thanks in advance for your help.

Update

In the question How to use HttpClient to send a Request from a specific IP address? C#, someone says "... the .net core team have implemented the HttpClientHandler etc without bothering to put anything related to service points in there ..." To confirm, I build the above code both with Framework4.7 and Core3.1 to find out that indeed, when built with Core, the callback is NOT called.

A Xamarin project targets .Net standard, which is a Core if I am not mistaken. I guess I need to forget this ServicePoint stuff and look for other solutions...

Again, your insight will be very much appreciated!

  • 1. Which platform you are trying to force IPv4 to HttpClient? 2. Have you tried to set the break point at `await _client.GetAsync(uri);` if confirm if it gets there? Also, you can try to use `response = await _client.GetAsync(uri); Console.WriteLine(response);` to see it on the debug output. 3. You can also refer to https://stackoverflow.com/a/37385163/9644964 and [Forcing HttpClient to use IPv4 or IPv6 addresses](https://www.meziantou.net/forcing-httpclient-to-use-ipv4-or-ipv6-addresses.htm) to see if you can get some help. – Alexandar May - MSFT Dec 16 '22 at 08:33
  • Thanks Alex. I want to do this on mobile phones, within an app that I am making with Xamarin. Yes, I confirm that the GetAsync() gets executed. [stackoverflow.com/a/37385163/9644964](https://stackoverflow.com/questions/37384945/how-to-force-ipv6-or-ipv4-for-httpwebrequest-or-webrequest-c-sharp/37385163#37385163) talks about a solution for httpwebrequet rather than httpclient, and another for .Net 5. [Forcing HttpClient to use IPv4 or IPv6 addresses](https://www.meziantou.net/forcing-httpclient-to-use-ipv4-or-ipv6-addresses.htm) is for .Net 5 too. – Kazutsugu Fukumuro Dec 16 '22 at 10:36
  • If the latter thread with .Net 5 works, you can try to update to .Net 5 and try to force using IPv4 with HttpClient. – Alexandar May - MSFT Dec 30 '22 at 13:01
  • Thanks Alex. If I am not wrong, it is not possible to upgrade (or change) a Xamarin Forms project to target Net5. – Kazutsugu Fukumuro Jan 08 '23 at 08:07

0 Answers0