0

I need to get data from another company's REST web service and I've never done this. I think I am on the right track but am not even sure about that. I know i am missing the full url (don't know where I should put it) and i don't know what to do if i successfully get the json results. Please please help.

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace GetData
{
    public class ReceiptHeader
    {
        private string _company;
        private string _warehouse;

        public ReceiptHeader()
        { }

        public string Company
        {
            get { return _company; }
            set { _company = value; }
        }
        public string Warehouse
        {
            get { return _warehouse; }
            set { _warehouse = value; }
        }
    }

    class Program
    {
        static HttpClient client = new HttpClient();

        static void ShowHeader(ReceiptHeader crh)
        {
            Console.WriteLine($"Company: {crh.Company}\tWarehouse: " +
                $"{crh.Warehouse}");
        }

        static async Task<ReceiptHeader> GetRHAsync(string path)
        {
            ReceiptHeader HeaderInfo = null;
            HttpResponseMessage response = await client.GetAsync(path);
            if (response.IsSuccessStatusCode)
            {
                HeaderInfo = await response.Content.ReadAsAsync<ReceiptHeader>();
            }
            return HeaderInfo;
        }

        static void Main()
        {
            RunAsync().GetAwaiter().GetResult();
        }

        static async Task RunAsync()
        {
            // Update port # in the following line.
            client.BaseAddress = new Uri("https://externalwebaddress.com/");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Authorization =
                new AuthenticationHeaderValue("Bearer", "TOKEN");

            try
            {
                // Get the header
                crh = await GetRHAsync(url.PathAndQuery);
                ShowHeader(crh);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            Console.ReadLine();
        }
    }
}
Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Rowan
  • 55
  • 1
  • 5
  • 1
    Does this answer your question? [How do I make calls to a REST api using C#?](https://stackoverflow.com/questions/9620278/how-do-i-make-calls-to-a-rest-api-using-c) – devNull Nov 07 '19 at 00:38
  • Making REST requests is a very common action in programming and someone has definitely done it before, meaning it's on stackoverflow. This is most likely going to be considered a duplicate question. You should look here: https://stackoverflow.com/questions/9620278/how-do-i-make-calls-to-a-rest-api-using-c – Issung Nov 07 '19 at 00:39

0 Answers0