Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.00 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. using System.Net.Http;
  7. using System.Text;
  8.  
  9. namespace APICallExample
  10. {
  11.     public class Promotion
  12.     {
  13.         public string store_name;
  14.         public string promotion_name;
  15.         public string brand;
  16.         public string description;
  17.         public string category;
  18.         public string price;
  19.         public string offer_type;
  20.         public string image_url;
  21.         public string product_id;
  22.     }
  23.  
  24.     class Program
  25.     {
  26.         static List<Promotion> promotions;
  27.         static readonly string api_key = "fabd8f7d-d193-4fdf-9afc-737cb5e05717";
  28.         static readonly string promotion_url = "http://18.185.249.107/pricepoint/api/promotions/";
  29.         static readonly string categorize_url = "http://18.185.249.107/pricepoint/api/promotions/categorize/";
  30.         private static readonly HttpClient client = new HttpClient();
  31.  
  32.         static void Main(string[] args)
  33.         {
  34.             Console.WriteLine("Hello World!");
  35.             POST(new Uri(string.Format(categorize_url)), "{\"product_id\":\"303237\", \"category\":\"Fisk\"}");
  36.  
  37.             /*
  38.             GET(promotion_url);
  39.             foreach (var promotion in promotions)
  40.             {
  41.                 Console.WriteLine(promotion.promotion_name);
  42.             }
  43.             */
  44.         }
  45.  
  46.         static void GET(string url)
  47.         {
  48.             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  49.             request.Method = "GET";
  50.             request.Headers.Add("Api-Key", api_key);
  51.  
  52.             try
  53.             {
  54.                 WebResponse response = request.GetResponse();
  55.                 using (Stream responseStream = response.GetResponseStream())
  56.                 {
  57.                     StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.UTF8);
  58.                     string json = reader.ReadToEnd();
  59.                     promotions = JsonConvert.DeserializeObject<List<Promotion>>(json);
  60.                 }
  61.             }
  62.             catch (WebException ex)
  63.             {
  64.                 WebResponse errorResponse = ex.Response;
  65.                 using (Stream responseStream = errorResponse.GetResponseStream())
  66.                 {
  67.                     StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.GetEncoding("utf-8"));
  68.                     String errorText = reader.ReadToEnd();
  69.                     // log errorText
  70.                 }
  71.             }
  72.         }
  73.  
  74.         static string POST(Uri url, string value)
  75.         {
  76.             var request = WebRequest.Create(url);
  77.             var byteData = Encoding.ASCII.GetBytes(value);
  78.             request.Headers.Add("Api-Key", api_key);
  79.             request.ContentType = "application/json";
  80.             request.Method = "POST";
  81.  
  82.             try
  83.             {
  84.                 using (var stream = request.GetRequestStream())
  85.                 {
  86.                     stream.Write(byteData, 0, byteData.Length);
  87.                 }
  88.                 var response = (HttpWebResponse)request.GetResponse();
  89.                 var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
  90.  
  91.                 return responseString;
  92.             }
  93.             catch (WebException e)
  94.             {
  95.                 return null;
  96.             }
  97.         }
  98.  
  99.  
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement