ASP.NET MVC 4 到 ASP.NET Web API - 错误:只允许使用“http”和“https”方案。参数名称:requestUri

man*_*u97 2 asp.net asp.net-mvc-4 asp.net-web-api

ASP.NET MVC 在 TEST Server 中引发以下错误,我无法在 Developer 机器中重现该错误。

尝试从 MVC 连接到 WebAPI 服务并收到错误

只允许使用“http”和“https”方案。参数名称:requestUri

我无法从其他问题中得到答案,因为他们主要是在谈论 WCF 绑定。

代码:

var credentials = "username:Password";
var encoded = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(credentials));
this.client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", encoded);
System.Net.Http.HttpClient client = new HttpClient();
HttpResponseMessage response = client.GetAsync(URL).Result; 
Run Code Online (Sandbox Code Playgroud)

注意:通过浏览器访问 WebAPI URL 会返回数据。

Adr*_*ips 6

考虑到您的URL参数中可能包含或不包含的内容,我猜它不是以http://or开头的https://。为此,它必须从这两者之一开始。

因此,如果您URL当前读取(假设URL是 a string):

mycompany.com/myservices
Run Code Online (Sandbox Code Playgroud)

它需要更改为:

http://mycompany.com/myservices
Run Code Online (Sandbox Code Playgroud)

或者

https://mycompany.com/myservices
Run Code Online (Sandbox Code Playgroud)

如果URL不是string,而是 的实例System.Uri,则在创建此对象时需要做类似的事情,或设置URL.Scheme.