Http获取请求未获取任何数据 [英] Http Get Request not getting any data

查看:101
本文介绍了Http获取请求未获取任何数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将我的Web Api在线安装在生产服务器上,到目前为止,在邮递员和Xamarin表单中都可以正常使用,直到我需要执行获取请求"并且不返回任何数据为止.实际上,它停止在 GetAsStringAsync 行,并且不会继续.相反,它跳出了方法,然后仅此而已.

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.

有人知道这个问题可能是什么吗?我已经检查并确保我的Internet和Uri也都在工作.

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

这是我在Xamarin表单中进行获取的地方:

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;
                    }
                }
        }

解决方案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;
    }  

这篇关于Http获取请求未获取任何数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆