static async Task Main(string[] args)
{
const int numberOfProducts = 3;
try
{
for (var i = 0; i < numberOfProducts; i++)
{
var product = "000000001585";
//DEV
var request =
(HttpWebRequest)WebRequest.Create("http://test//api/v2/purchase");
var svcCredentials =
Convert.ToBase64String(
Encoding.ASCII.GetBytes("username" + ":" + "password"));
request.Headers.Add("Authorization", "Basic " + svcCredentials);
request.Method = "POST";
request.KeepAlive = false;
request.ContentType = "application/x-www-form-urlencoded";
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("productCode", product),
new KeyValuePair<string, string>("quantity", "1"),
new KeyValuePair<string, string>("shopNo", "test"),
new KeyValuePair<string, string>("safeNo", "test"),
new KeyValuePair<string, string>("cashierNo", "test")
});
var formUrlEncodedContent = content;
var urlEncodedString = await formUrlEncodedContent.ReadAsStringAsync();
using (var streamWriter = new StreamWriter(await request.GetRequestStreamAsync()))
{
await streamWriter.WriteAsync(urlEncodedString);
}
var httpResponse = (HttpWebResponse)(await request.GetResponseAsync());
var response = new HttpResponseMessage
{
StatusCode = httpResponse.StatusCode,
Content = new StreamContent(httpResponse.GetResponseStream()),
};
//Read response
var htmlResponse = await response.Content.ReadAsStringAsync();
var deserializedResult = JObject.Parse(htmlResponse);
Console.WriteLine((string)deserializedResult["coupons"]?[0]?["Serial"] + ":" + (string)deserializedResult["coupons"]?[0]?["Pin"]);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw;
}
}