Skip to content

Instantly share code, notes, and snippets.

@thehoneymad
Created September 11, 2016 13:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thehoneymad/54aa5abc07847f26474fe56aa6ee08e3 to your computer and use it in GitHub Desktop.
Save thehoneymad/54aa5abc07847f26474fe56aa6ee08e3 to your computer and use it in GitHub Desktop.
HttpClient Sample on posting data.
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
public class Program
{
public static void Main()
{
var task = Login();
task.Wait();
}
private static async Task Login()
{
var client = new HttpClient();
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("username", "alice@example.com"),
new KeyValuePair<string, string>("password", "password1")
});
var result = await client.PostAsync("https://localhost:44305/token", content).ConfigureAwait(false);
string resultContent = await result.Content.ReadAsStringAsync();
var tokenResponse = JsonConvert.DeserializeObject<TokenResponse>(resultContent);
}
}
public class TokenResponse
{
public string access_token { get; set; }
public string token_type { get; set; }
public int expires_in { get; set; }
public string userName { get; set; }
public string issued { get; set; }
public string expires { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment