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