Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid URL exception when calling endpoint with port in .NET Core

Tags:

c#

.net

.net-core

I'm working on a large .NET Core project where we have the need to make REST requests from C# to specific server endpoints.

The endpoints are specified in the format http://some.domain.com:70.

When called, an exception of type System.Net.Http.HttpRequestException is thrown, with the message The URL is invalid.

When calling endpoints that omit the port in the address, it works fine. It also works fine when running it in .NET Framework 4.6 instead of .NET Core.

Since almost all 3rd party libraries that could simplify these outgoing calls relies on System.Net.Http, the problem remains.

Below is a minimum working example that can be tried as a console application (exception thrown in .NET Core, works fine in .NET Framework 4.6)

using System;

    namespace NetCoreConsoleTestREST
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Calling service...");
                Test();
                Console.ReadKey();
            }


            public static async void Test()
            {
                var client = new System.Net.Http.HttpClient();
                var res = await client.GetAsync(new Uri("http://some.domain.com:70")); //This doesn't work in .NET Core
                //var res = await client.GetAsync(new Uri("https://requestb.in/wy9h8gwy")); //This works perfectly fine

                Console.WriteLine(res);
            }
        }
    }

The .NET Core versions I have installed

Microsoft .NET Core Shared Framework Host

  Version  : 1.1.0
  Build    : 928f77c4bc3f49d892459992fb6e1d5542cb5e86

Is this a bug in .NET CoreFX or by design?


Update It will only fail if I use the port number 70. If I switch to i.e 1234 it works.

like image 505
Daniel B Avatar asked Nov 25 '25 07:11

Daniel B


1 Answers

As far as I can tell, this is specifically caused by using port 70. HttpClient on .Net Core on Windows uses WinHTTP. And What's New in WinHTTP 5.1 says (emphasis mine):

Starting with Windows Server 2003 and Windows XP with SP2, sending requests to the following well-known, non-HTTP, ports is restricted for security reasons: 21 (FTP), 25 (SMTP), 70 (GOPHER), 110 (POP3), 119 (NNTP), 143 (IMAP).

like image 152
svick Avatar answered Nov 26 '25 23:11

svick



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!