private static void RunClient() { // TEST CERTIFICATE ONLY, PLEASE REMOVE WHEN YOU REPLACE THE CERT WITH A REAL CERT // Perform the Server Certificate validation ServicePointManager.ServerCertificateValidationCallback += Program.RemoteCertificateValidationCallback; // start the client WebRequestHandler handler = new WebRequestHandler(); handler.ClientCertificateOptions = ClientCertificateOption.Manual; // this would pick from the Current user store handler.ClientCertificates.Add(GetClientCertificate()); HttpClient client = new HttpClient(handler); client.BaseAddress = _baseAddress; HttpResponseMessage response; // How to post a sample item with success SampleItem sampleItem = new SampleItem { Id = 1, StringValue = "hello from a new sample item" }; response = client.PostAsJsonAsync("/api/sample/", sampleItem).Result; response.EnsureSuccessStatusCode(); Console.WriteLine("Posting the first item returns:\n" + response.Content.ReadAsStringAsync().Result + "\n"); response.Dispose(); // How to get all the sample Items, we should see the newly added sample item that we just posted response = client.GetAsync("/api/sample/").Result; response.EnsureSuccessStatusCode(); Console.WriteLine("Getting all the items returns:\n" + response.Content.ReadAsStringAsync().Result + "\n"); response.Dispose(); // How to post another sample item with success sampleItem = new SampleItem { Id = 2, StringValue = "hello from a second new sample item" }; response = client.PostAsJsonAsync("/api/sample/", sampleItem).Result; response.EnsureSuccessStatusCode(); Console.WriteLine("Posting a second item returns:\n" + response.Content.ReadAsStringAsync().Result + "\n"); response.Dispose(); // How to get all the sample Items, we should see the two newly added sample item that we just posted response = client.GetAsync("/api/sample/").Result; response.EnsureSuccessStatusCode(); Console.WriteLine("Getting all the items returns:\n" + response.Content.ReadAsStringAsync().Result + "\n"); response.Dispose(); // How to get an sample item with id = 2 response = client.GetAsync("/api/sample/2").Result; response.EnsureSuccessStatusCode(); Console.WriteLine("Getting the second item returns:\n" + response.Content.ReadAsStringAsync().Result + "\n"); response.Dispose(); }
public string PostItem(SampleItem item) { sampleItems.Enqueue(item); return item == null ? "null" : item.StringValue; }