Advertisement
melisa123

the put method

Dec 31st, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. public static async Task Put(string postData)
  2. {
  3. using(var httpClient = new HttpClient())
  4. {
  5. string authUserName = "user";
  6. string authPassword = "password"
  7. string url = "https://someurl.com";
  8.  
  9. // If you have no basic authentication, you can skip thses lines
  10. var authToken = Encoding.ASCII.GetBytes($"{authUserName}:{authPassword}");
  11. httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(authToken));
  12.  
  13. // The actual put method including error handling
  14. using(var content = new StringContent(postData))
  15. {
  16. var result = await httpClient.PutAsync($"{url}", content);
  17. if (result.StatusCode == HttpStatusCode.OK)
  18. {
  19. return;
  20. }
  21. else
  22. {
  23. // Do something with the contents, like write the statuscode and
  24. // contents to a log file
  25. string resultContent = await result.Content.ReadAsStringAsync();
  26. // ... write to log
  27. }
  28. }
  29. }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement