Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Net;
- using System.Net.Http;
- using System.Text;
- namespace APICallExample
- {
- public class Promotion
- {
- public string store_name;
- public string promotion_name;
- public string brand;
- public string description;
- public string category;
- public string price;
- public string offer_type;
- public string image_url;
- public string product_id;
- }
- class Program
- {
- static List<Promotion> promotions;
- static readonly string api_key = "fabd8f7d-d193-4fdf-9afc-737cb5e05717";
- static readonly string promotion_url = "http://18.185.249.107/pricepoint/api/promotions/";
- static readonly string categorize_url = "http://18.185.249.107/pricepoint/api/promotions/categorize/";
- private static readonly HttpClient client = new HttpClient();
- static void Main(string[] args)
- {
- Console.WriteLine("Hello World!");
- POST(new Uri(string.Format(categorize_url)), "{\"average_time\":\"9000\"}");
- /*
- GET(promotion_url);
- foreach (var promotion in promotions)
- {
- Console.WriteLine(promotion.promotion_name);
- }
- */
- }
- static void GET(string url)
- {
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
- request.Method = "GET";
- request.Headers.Add("Api-Key", api_key);
- try
- {
- WebResponse response = request.GetResponse();
- using (Stream responseStream = response.GetResponseStream())
- {
- StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.UTF8);
- string json = reader.ReadToEnd();
- promotions = JsonConvert.DeserializeObject<List<Promotion>>(json);
- }
- }
- catch (WebException ex)
- {
- WebResponse errorResponse = ex.Response;
- using (Stream responseStream = errorResponse.GetResponseStream())
- {
- StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.GetEncoding("utf-8"));
- String errorText = reader.ReadToEnd();
- // log errorText
- }
- }
- }
- static string POST(Uri url, string value)
- {
- var request = WebRequest.Create(url);
- var byteData = Encoding.ASCII.GetBytes(value);
- request.ContentType = "application/json";
- request.Method = "POST";
- try
- {
- using (var stream = request.GetRequestStream())
- {
- stream.Write(byteData, 0, byteData.Length);
- }
- var response = (HttpWebResponse)request.GetResponse();
- var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
- return responseString;
- }
- catch (WebException e)
- {
- return null;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement