Monday, 6 August 2018

C# REST Server POST and Get Response

async Task Main()
{
    string outputfile = "output.csv";
  
    File.Delete (outputfile);
  
    var httpWebRequest = (HttpWebRequest)WebRequest.Create(URL);
    httpWebRequest.ContentType = "application/json";
    httpWebRequest.Method = "POST";
  
    using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
    {
        string json = new JavaScriptSerializer().Serialize(new
        {
            startDate = "7/1/2018",
            endDate = "6/30/2019",
            codes = new string[] { "value1", "value2" },
            skus= "",
            returnCSV = 1
        });
        streamWriter.Write(json);
    }

    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
        using (StreamWriter sw = new StreamWriter(outputfile))
            sw.Write(await streamReader.ReadToEndAsync());
    }
}

No comments:

SQL: Generate a range of numbers

SELECT ones.n + 10*tens.n + 100*hundreds.n + 1000*thousands.n FROM       (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) ones(n),      (VALU...