Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.80 KB | None | 0 0
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Net;
  6.  
  7. namespace APICallExample
  8. {
  9.     public class Promotion
  10.     {
  11.         public string store_name;
  12.         public string promotion_name;
  13.         public string brand;
  14.         public string description;
  15.         public string category;
  16.         public string price;
  17.         public string offer_type;
  18.         public string image_url;
  19.         public string product_id;
  20.     }
  21.  
  22.     class Program
  23.     {
  24.         static List<Promotion> promotions;
  25.         static readonly string api_key = "fabd8f7d-d193-4fdf-9afc-737cb5e05717";
  26.         static readonly string promotion_url = "http://18.185.249.107/pricepoint/api/promotions/";
  27.  
  28.  
  29.         static void Main(string[] args)
  30.         {
  31.             Console.WriteLine("Hello World!");
  32.  
  33.             GET(promotion_url);
  34.             foreach (var promotion in promotions)
  35.             {
  36.                 Console.WriteLine(promotion.promotion_name);
  37.             }
  38.         }
  39.  
  40.         static void GET(string url)
  41.         {
  42.             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  43.             request.Method = "GET";
  44.             request.Headers.Add("Api-Key", api_key);
  45.  
  46.             try
  47.             {
  48.                 WebResponse response = request.GetResponse();
  49.                 using (Stream responseStream = response.GetResponseStream())
  50.                 {
  51.                     StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.UTF8);
  52.                     string json = reader.ReadToEnd();
  53.                     promotions = JsonConvert.DeserializeObject<List<Promotion>>(json);
  54.                 }
  55.             }
  56.             catch (WebException ex)
  57.             {
  58.                 WebResponse errorResponse = ex.Response;
  59.                 using (Stream responseStream = errorResponse.GetResponseStream())
  60.                 {
  61.                     StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.GetEncoding("utf-8"));
  62.                     String errorText = reader.ReadToEnd();
  63.                     // log errorText
  64.                 }
  65.             }
  66.         }
  67.  
  68.        
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement