SHARE
TWEET

Untitled

a guest Nov 13th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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)), "{\"average_time\":\"9000\"}");
  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.ContentType = "application/json";
  79.             request.Method = "POST";
  80.  
  81.             try
  82.             {
  83.                 using (var stream = request.GetRequestStream())
  84.                 {
  85.                     stream.Write(byteData, 0, byteData.Length);
  86.                 }
  87.                 var response = (HttpWebResponse)request.GetResponse();
  88.                 var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
  89.  
  90.                 return responseString;
  91.             }
  92.             catch (WebException e)
  93.             {
  94.                 return null;
  95.             }
  96.         }
  97.  
  98.  
  99.     }
  100. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top