Skip to content

Instantly share code, notes, and snippets.

@AvgustPol
Last active October 26, 2020 10:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AvgustPol/8e14a07862ae5d804b8b7a10398c681d to your computer and use it in GitHub Desktop.
Save AvgustPol/8e14a07862ae5d804b8b7a10398c681d to your computer and use it in GitHub Desktop.
WebClient cheatsheet
public string HttpGet<T>(string url, string publicAccessToken)
{
using (WebClient wc = new WebClient())
{
wc.Headers.Add("Authorization", $"Basic {publicAccessToken}");
string reply = await wc.DownloadStringTaskAsync(url);
var result = JsonConvert.DeserializeObject<T>(reply);
return result;
}
}
public string HttpPost<T>(string url, T object)
{
using (WebClient wc = new WebClient())
{
string objectApi = JsonConvert.SerializeObject(object);
wc.QueryString.Add("body", objectApi);
// POST
var data = wc.UploadString(url, objectApi);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment