本地主机ASP.Net核心API调用Android模拟器

问题描述:

我一直在尝试执行从Visual Studio的Android模拟器到本地主机上运行的ASP.Net核心API的GET请求。我第一次看,我有执行从Android的请求时使用的IP 10.0.2.2,所以我有以下HTTP客户端代码在我的Android(Xamarin)应用本地主机ASP.Net核心API调用Android模拟器

protected override void OnCreate(Bundle bundle) 
     { 
      base.OnCreate(bundle); 
      SetContentView (Resource.Layout.Main); 

      var logInbutton = FindViewById<Button>(Resource.Id.logInButton); 

      logInbutton.Click += OnLogInButtonClicked; 
     } 

     private async void OnLogInButtonClicked(object sender, EventArgs e) 
     { 
      using (var client = new HttpClient()) 
      { 
       client.DefaultRequestHeaders.Accept.Clear(); 
       client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 


       var uri = new Uri("http://10.0.2.2:5555/api/Orders?api-version=0.9"); 
       var response = await client.GetAsync(uri); 
      } 
     } 

然而,每一个我试图运行此我的时间米迎来一个400 Bad Request错误。我知道请求没问题,因为我可以从控制台应用程序运行相同的Http客户端代码而不会出现任何错误。然后我读到IIS express不接受外部请求,并且由于Android模拟器在VM中运行,这可能是原因。

因此,我随后就这SO post的意见,但与Invalid URI: The hostname could not be parsed错误迎接。我不知道如何从这里开始。有没有对Invalid URI: The hostname could not be parsed错误的解决方案,还是应该尝试绕过IIS Express并单独使用kestrel进行开发?甚至只用隼来运行解决这个问题?

我有类似的问题。

首先,我们找到正确的applicationhost.config文件。 您应该检查这个位置:

[solution folder]/.vs/config/applicationhost.config 

于是人们建议设立这样的结合:

<binding protocol="http" bindingInformation="*:5555:*" /> 

它没有为我工作。我收到“无效的URI:无法解析主机名”。

什么,我所做的工作是这样的语法:

<binding protocol="http" bindingInformation=":5555:" /> 

请注意,端口给出的例子。你应该使用服务器正在监听的端口。