0

我一直在嘗試執行從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進行開發?甚至只用隼來運行解決這個問題?

回答

0

我有類似的問題。

首先,我們找到正確的applicationhost.config文件。 您應該檢查這個位置:

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

於是人們建議設立這樣的結合:

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

它沒有爲我工作。我收到「無效的URI:無法解析主機名」。

什麼,我所做的工作是這樣的語法:

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

請注意,端口給出的例子。你應該使用服務器正在監聽的端口。

相關問題