Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static async Task Put(string postData)
- {
- using(var httpClient = new HttpClient())
- {
- string authUserName = "user";
- string authPassword = "password"
- string url = "https://someurl.com";
- // If you have no basic authentication, you can skip thses lines
- var authToken = Encoding.ASCII.GetBytes($"{authUserName}:{authPassword}");
- httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(authToken));
- // The actual put method including error handling
- using(var content = new StringContent(postData))
- {
- var result = await httpClient.PutAsync($"{url}", content);
- if (result.StatusCode == HttpStatusCode.OK)
- {
- return;
- }
- else
- {
- // Do something with the contents, like write the statuscode and
- // contents to a log file
- string resultContent = await result.Content.ReadAsStringAsync();
- // ... write to log
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement