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