Http Get Request not getting any data

谁说我不能喝 提交于 2021-01-29 08:00:37

问题


I have my Web Api on a production server online and working well in postman and in Xamarin forms so far until I needed to do a Get Request and does not return any data. Infact it stops at the GetAsStringAsync line and does not continue. Instead, it jumps out of the method and then nothing more.

Does any one know what the problem could be? I have checked and made sure my Internet is working and the Uri too.

This is where I am doing my Get in Xamarin forms:

public async Task<List<OfferModel>> AllOffers()
{
    var httpclient = new HttpClient();
    httpclient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", Settings.AccessToken);
    //it does not continue after this line, it jumps out of the method instead
    var response = await httpclient.GetStringAsync(UrlConstants.offerurl);
    var data =JsonConvert.DeserializeObject<List<OfferModel(response);
    return data;
}

回答1:


Solution 1

Can you try access task via awaiter it may be wait until result when responded

    public class HttpHelperService
    {
                public async Task<List<OfferModel>> AllOffers()
                {
                    List<OfferModel> result;
                    string responseBody;
                    using (HttpClient client = new HttpClient())
                    {
                        try
                        {
                            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", Settings.AccessToken);
                            HttpResponseMessage response = client.GetStringAsync(new Uri(UrlConstants.offerurl)).GetAwaiter().GetResult();
                            result = JsonConvert.DeserializeObject<List<OfferModel>>(response);
                        }
                        catch (Exception ex)
                        {
                            result = null;
                        }
                        return result;
                    }
                }
        }

Solution 2

public class MyPage : ContentPage
{
//Here is your page constructor
    public MyPage()
    {
       GetServices(); //--> call here without awaiter
    }
}

//Here is your awaiter method
    private async void GetServices()
    {
       LoadingPopupService.Show();
       var result = await HttpService.AllOffers();
        LoadingPopupService.Hide();
    }

//Here is your service.
    public async Task<List<OfferModel>> AllOffers()
    {
        var httpclient = new HttpClient();
        httpclient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", Settings.AccessToken);
        var response = await httpclient.GetStringAsync(UrlConstants.offerurl);
        var data =JsonConvert.DeserializeObject<List<OfferModel(response);
        return data;
    }  


来源:https://stackoverflow.com/questions/55326677/http-get-request-not-getting-any-data

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