//PUT call static async Task <KeyValuePair <JObject, string> > RunPutAsync(Uri qUri, HttpContent contentToPut) { try { // ... Use HttpClient. WebRequestHandlerWithClientcertificates handler = new WebRequestHandlerWithClientcertificates(); handler.ClientCertificates.Add(cert); using (HttpClient client = new HttpClient(handler)) using (HttpResponseMessage response = await client.PutAsync(qUri, contentToPut)) { JObject jResponse = JObject.FromObject(response); string content = await response.Content.ReadAsStringAsync(); return(new KeyValuePair <JObject, string>(jResponse, content)); } } catch (Exception e) { Console.WriteLine("PUT request failed."); Console.WriteLine("URL: " + qUri.ToString()); Console.WriteLine("Content: " + contentToPut.ToString()); Console.WriteLine(e); return(new KeyValuePair <JObject, string>(null, null)); } }
//POST call static async Task <KeyValuePair <JObject, string> > RunPostAsync(Uri qUri, Object contentToPush) { try { // ... Use HttpClient. WebRequestHandlerWithClientcertificates handler = new WebRequestHandlerWithClientcertificates(); handler.ClientCertificates.Add(cert); using (HttpClient client = new HttpClient(handler)) { client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); // Newtonsoft Json serialization Console.WriteLine(contentToPush.ToString()); var upContent = JObject.FromObject(contentToPush); Console.WriteLine(upContent.ToString()); var strContent = new System.Net.Http.StringContent(upContent.ToString(), Encoding.UTF8, "application/json"); using (HttpResponseMessage response = await client.PostAsync(qUri, strContent)) { JObject jResponse = JObject.FromObject(response); string content = await response.Content.ReadAsStringAsync(); return(new KeyValuePair <JObject, string>(jResponse, content)); } } return(new KeyValuePair <JObject, string>(null, null)); } catch (Exception e) { Console.WriteLine("POST request failed."); Console.WriteLine("URL: " + qUri.ToString()); Console.WriteLine("Content: " + contentToPush.ToString()); Console.WriteLine(e); return(new KeyValuePair <JObject, string>(null, null)); } }