Httpclient Slow Performance same computer Android Emulator Xamarin

╄→гoц情女王★ 提交于 2021-01-29 05:31:11

问题


I am using the HttpClient but my results are taking up to 6 seconds coming back from the same machine on the same subnet and ip range of 192.168. When I call the api directly from the ip address the results are more or less instant so why is it so slow with httpclient on the same computer.

I have seen other so's that suggest set to use proxy as false is the best way to go.

I have also tested this on a stock phone and it takes around 8 seconds for the login to be successful on the phone.

private HttpClient _client;
public async Task<String> Getusers()
{
        var content = "";
        HttpClientHandler hch = new HttpClientHandler();
        hch.Proxy = null;
        hch.UseProxy = false;
        _client = new HttpClient(hch);
        var uri = new Uri(Constants.ApiEndPoint + "/Users"); // Your url is here

        try
        {
            var response = await _client.GetAsync(uri);
            if (response.IsSuccessStatusCode)
            {
                content = await response.Content.ReadAsStringAsync();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }

        return content;
}

Here is my login method in case anyone can see something wrong with it.

private async void BtnLogin_Clicked(object sender, EventArgs e)
{
    string content = await Getusers(); //Sends a GET request to the specified Uri and returns the response body as a string in an asynchronous operation
    List<Users> _users = JsonConvert.DeserializeObject<List<Users>>(content); //Deserializes or converts JSON String into a collection of Post
    var userName = txtUserName.Text;
    var password = txtPassword.Text;
    var isValidUser = _users.Where(w => w.UserName == userName && w.password == password).FirstOrDefault();
    var driverId = _users.Where(w => w.UserName == userName && w.password == password).FirstOrDefault().ID;         


    if (isValidUser != null)
    {

            Application.Current.Properties["driverId"] = driverId;
            Application.Current.MainPage = new MainPage();
    }
    else
    {
            lblError.Text = "Error your credentials are invalid, please try again";
    }         

  }

来源:https://stackoverflow.com/questions/55894813/httpclient-slow-performance-same-computer-android-emulator-xamarin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!