无法在主机地址中为异步 api 调用构建带有“:”的 uri

Unable to build uri with ":" in host address for async api call

我正在尝试使用 HttpClient.PutAsync 对外部 api 进行 put 调用,其地址在主机名中包含“:”,例如“https://xx-xx-xxx:000/orders/create”。

如果我直接将url字符串传递给HttpClient.PutAsync(apiUrl,apiContent)或者构建一个Uri传递给它,主机名在“:”处被截断,例如“https://xx-xx-xxxx/orders/create”。如果我尝试使用 UriBuilder 将“:”强制放入 Uri,它会抛出以下异常:"Invalid URI: The hostname could not be parsed."

我是否需要构建自定义 UriParser class 才能执行此操作?如果是这样,我不确定如何解决这个问题以及在哪里注册它。

HttpResponseMessage response = new HttpResponseMessage();

        try
        {
            //this causes exception
            UriBuilder builder = new UriBuilder();
            builder.Scheme = "https";
            builder.Host = "xx-xx-xx00:000";
            builder.Path = "/orders/create";
            Uri apiUri = builder.Uri;

            //this gets truncated if passed into PutAsync
            string apiUrl = "https://xx-xx-xx00:000/orders/create";

            //this also truncates
            Uri uri = new Uri(apiUrl);

            HttpClientHandler handler = new HttpClientHandler();
            handler.ServerCertificateCustomValidationCallback = delegate { return true; };

            HttpClient client = new HttpClient(handler);
            client.DefaultRequestHeaders.Clear();

            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", encodedUidPwd);


            response = await client.PutAsync(apiUri, apiContent);

        }

编辑:显然这只是一个 3 位端口号,即“https://xx-xx-xxx:0000/orders/create”不会被截断。但是,我相当确定我尝试访问的端口有 3 位数字

尝试在 UriBuilder 的 Port 属性 中设置端口号,而不是将其放在 Host 属性 中。更多信息 here.