Skip to main content
Decrease indentation
Source Link
James Skemp
  • 8.6k
  • 9
  • 71
  • 116

Use Basic Authorization And Json Parameters.

using (HttpClient client = new HttpClient())  {   var request_json = "your json string";   var content = new StringContent(request_json, Encoding.UTF8, "application/json");   var authenticationBytes = Encoding.ASCII.GetBytes("YourUsername:YourPassword");   client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",   Convert.ToBase64String(authenticationBytes));   client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));   var result = await client.PostAsync("YourURL", content);   var result_string = await result.Content.ReadAsStringAsync();  } 

Use Basic Authorization And Json Parameters.

using (HttpClient client = new HttpClient())  {   var request_json = "your json string";   var content = new StringContent(request_json, Encoding.UTF8, "application/json");   var authenticationBytes = Encoding.ASCII.GetBytes("YourUsername:YourPassword");   client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",   Convert.ToBase64String(authenticationBytes));   client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));   var result = await client.PostAsync("YourURL", content);   var result_string = await result.Content.ReadAsStringAsync();  } 

Use Basic Authorization And Json Parameters.

using (HttpClient client = new HttpClient()) { var request_json = "your json string"; var content = new StringContent(request_json, Encoding.UTF8, "application/json"); var authenticationBytes = Encoding.ASCII.GetBytes("YourUsername:YourPassword"); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(authenticationBytes)); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var result = await client.PostAsync("YourURL", content); var result_string = await result.Content.ReadAsStringAsync(); } 
removed dangerous (and unnecessary) lines
Source Link
ProgrammingLlama
  • 39.4k
  • 7
  • 79
  • 105

Use Basic Authorization And Json Parameters.

using (HttpClient client = new HttpClient()) { var request_json = "your json string"; var content = new StringContent(request_json, Encoding.UTF8, "application/json"); var authenticationBytes = Encoding.ASCII.GetBytes("YourUsername:YourPassword"); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(authenticationBytes)); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true; var result = await client.PostAsync("YourURL", content); var result_string = await result.Content.ReadAsStringAsync(); } 

Use Basic Authorization And Json Parameters.

using (HttpClient client = new HttpClient()) { var request_json = "your json string"; var content = new StringContent(request_json, Encoding.UTF8, "application/json"); var authenticationBytes = Encoding.ASCII.GetBytes("YourUsername:YourPassword"); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(authenticationBytes)); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true; var result = await client.PostAsync("YourURL", content); var result_string = await result.Content.ReadAsStringAsync(); } 

Use Basic Authorization And Json Parameters.

using (HttpClient client = new HttpClient()) { var request_json = "your json string"; var content = new StringContent(request_json, Encoding.UTF8, "application/json"); var authenticationBytes = Encoding.ASCII.GetBytes("YourUsername:YourPassword"); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(authenticationBytes)); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var result = await client.PostAsync("YourURL", content); var result_string = await result.Content.ReadAsStringAsync(); } 
Source Link
Mohammad Soori
  • 2.4k
  • 1
  • 20
  • 17

Use Basic Authorization And Json Parameters.

using (HttpClient client = new HttpClient()) { var request_json = "your json string"; var content = new StringContent(request_json, Encoding.UTF8, "application/json"); var authenticationBytes = Encoding.ASCII.GetBytes("YourUsername:YourPassword"); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(authenticationBytes)); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true; var result = await client.PostAsync("YourURL", content); var result_string = await result.Content.ReadAsStringAsync(); }