Saya ingin membuat aplikasi konsol di c# untuk mendapatkan data (detail berita google) dari google-news-api. Saya dicoba seperti ini

static void Main(string[] args)
    {
        using (var _http = new HttpClient())
        {
            _http.BaseAddress = new Uri("https://newsapi.org/v2/");
            _http.DefaultRequestHeaders.Clear();
            _http.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            HttpResponseMessage response = await _http.GetAsync("everything?sources=&q=bbc-news&sortBy=publishedAt&pageSize=80&apiKey=4dbc17e007ab436fb66416009dfb59a8");
            response.EnsureSuccessStatusCode();
            using (HttpContent content = response.Content)
            {

            }
        }
    }

Ref:-google news api

-1
Vikas 11 Maret 2020, 11:25

1 menjawab

Jawaban Terbaik

Semoga ini bisa membantu.

 static void Main(string[] args)
    {
        using (var _http = new HttpClient())
        {
            _http.BaseAddress = new Uri("https://newsapi.org/v2/");
            _http.DefaultRequestHeaders.Clear();
            _http.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            string response = MainAsync(_http).Result;

            //TODO: Handel the response string the way you wanted to 

        }
    }

    static  async Task<string> MainAsync(HttpClient _http)
    {
        HttpResponseMessage response =  await _http.GetAsync("everything?sources=&q=bbc-news&sortBy=publishedAt&pageSize=80&apiKey=4dbc17e007ab436fb66416009dfb59a8");
        return await response.Content.ReadAsStringAsync();
    }
1
Asad 12 Maret 2020, 08:12