1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
using Newtonsoft.Json;
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace HttpClientSample
{
public class Params
{
public string username { get; set; }
public string password { get; set; }
}
class Program
{
static HttpClient client = new HttpClient();
static async Task<Uri> CreateAuthAsync(Params para)
{
HttpResponseMessage response = await client.PostAsJsonAsync("rest/auth",para);
response.EnsureSuccessStatusCode();
Console.WriteLine("status de la requete : " + response.StatusCode);
Console.WriteLine(response.RequestMessage);
return response.Headers.Location;
}
static void Main()
{
RunAsync().Wait();
}
static async Task RunAsync()
{
client.BaseAddress = new Uri("https://api.devicewise.com/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
try
{
Params para = new Params { username = "username", password = "password" };
var url = await CreateAuthAsync(para);
Console.WriteLine("url entier : "+url+" : fin de l'url");
Console.WriteLine(url.PathAndQuery);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.ReadLine();
}
}
} |
Partager