Xamarin.Forms System.Net.WebException中的超时异常:<超时超出=“ =” getting =“” exception =“” details =“”>

时间:2019-05-09 12:08:27

标签: c# xamarin.forms

我正在尝试通过调用api在Xamarin应用中显示登录消息...但它显示

未处理的异常:

  

System.Net.WebException:   发生

在此行

var response =await httpClient.GetStringAsync("http://localhost:40987/Api/Home/Post?username="+username.Text+"&password="+password.Text);

我尝试了所有建议,但没有摆脱困境

    private async void Button_ClickedAsync(object sender, EventArgs e)
    {
        User user = new User
        {
            UserName = username.Text,
            Password = password.Text

        };


        var Json = Newtonsoft.Json.JsonConvert.SerializeObject(user);
        HttpContent httpContent = new StringContent(Json);
        httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/Json");
        var httpClient = new HttpClient();
        //httpClient.GetAsync("windows.digitalgramsoft.com/Api/Home");
        //DisplayAlert("Added", "Your Data has been added", "OK")
        var response =await httpClient.GetStringAsync("http://localhost:40987/Api/Home/Post?username="+username.Text+"&password="+password.Text);


        var login = JsonConvert.DeserializeObject<List<User>>(response);

    }

只想使用api显示登录消息或错误消息

1 个答案:

答案 0 :(得分:0)

尝试一下:

    try{
     var Json = Newtonsoft.Json.JsonConvert.SerializeObject(user);
            HttpContent httpContent = new StringContent(Json);
            httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/Json");
            var httpClient = new HttpClient();
            //httpClient.GetAsync("windows.digitalgramsoft.com/Api/Home");
            //DisplayAlert("Added", "Your Data has been added", "OK")
            var response =await httpClient.GetStringAsync("http://localhost:40987/Api/Home/Post?username="+username.Text+"&password="+password.Text);


            var login = JsonConvert.DeserializeObject<List<User>>(response);
    }catch(Exception ex){
    Console.WriteLine(ex.Message);
}

然后使用断点检查Exception对象中的内容。从那里,您可以跟踪源。