Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing an IPv6 loopback address to a Uri

I've written a client/server application which I'm testing on my local machine.

The server reads the IP Address of the client as ::1 which is an IPv6 loopback address.

However, I need to re-establish an HttpClient connection to this client later, by providing a Uri's BaseAddress:

  var originalAddress = "::1";
  var client = new HttpClient() { BaseAddress = new Uri(originalAddress) };

This results in the exception Invalid URI: The format of the URI could not be determined.

How can I get the Uri to accept this address?

like image 381
Daniel Minnaar Avatar asked Oct 19 '25 05:10

Daniel Minnaar


1 Answers

As mentioned in other posts, the solution is to wrap brackets around the IPv6 address in accordance with RFC 2732:

var uri = new Uri("http://[::1]:8080");
like image 115
Daniel Minnaar Avatar answered Oct 22 '25 06:10

Daniel Minnaar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!