Created
September 11, 2016 13:41
-
-
Save thehoneymad/54aa5abc07847f26474fe56aa6ee08e3 to your computer and use it in GitHub Desktop.
HttpClient Sample on posting data.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", "[email protected]"), | |
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