C# HttpClient RestAPI Patch
public static async Task<string> WDDCAsync(string bearer, string id, string val, bool isReturnResponseMessage = false)
{
//ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; // 인증
try
{
var cl = new HttpClient();
cl.BaseAddress = new Uri(BaseUrl);
cl.Timeout = TimeSpan.FromSeconds(3);
cl.DefaultRequestHeaders.Add("authorization", "Bearer " + bearer);
WddcRequestBody body = new WddcRequestBody { value = val }; // 1 : ON, 0: OFF
var jsonString = body.ToJSON();
HttpContent content = new StringContent(jsonString, Encoding.UTF8, "application/+json");
HttpResponseMessage response = await cl.PatchAsync(new Uri(cl.BaseAddress), content);
if (isReturnResponseMessage)
{
return response.ToString();
}
if ((int)response.StatusCode == 204) // No Content
{
return "true";
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
return "false";
}
return "false";
}
public static class HttpClientExtensions
{
public static async Task<HttpResponseMessage> PatchAsync(this HttpClient client, Uri requestUri, HttpContent iContent)
{
var method = new HttpMethod("PATCH");
var request = new HttpRequestMessage(method, requestUri)
{
Content = iContent
};
HttpResponseMessage response = new HttpResponseMessage();
try
{
response = await client.SendAsync(request);
response.Content.Headers.Add("content-type", "application/+json");
}
catch (TaskCanceledException e)
{
Debug.WriteLine("ERROR: " + e.ToString());
}
return response;
}
}
'C# > Network' 카테고리의 다른 글
C# Ping 테스트, 해당 IP 장치 이름 가져오기 (0) | 2020.08.07 |
---|---|
C# 네트워크 상 맥어드레스 가져오기 (0) | 2020.08.07 |
C# http post header body (0) | 2020.08.07 |
C# HttpClient RestAPI Post (0) | 2020.08.05 |
C# HttpClient RestAPI Get (0) | 2020.08.03 |