첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.

728x90
반응형
728x170
using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

#region POST 요청 객체 구하기 (비동기) - GetPOSTRequestObjectAsync<TObject>(url, tokenSchema, token, input)

/// <summary>
/// POST 요청 객체 구하기 (비동기)
/// </summary>
/// <typeparam name="TObject">객체 타입</typeparam>
/// <param name="url">URL</param>
/// <param name="tokenSchema">토큰 스키마</param>
/// <param name="token">토큰</param>
/// <param name="input">입력 객체</param>
/// <returns>POST 요청 객체 태스크</returns>
public async Task<TObject> GetPOSTRequestObjectAsync<TObject>(string url, string tokenSchema, string token, object input)
{
    using HttpClient client = GetHTTPClient(false, url, tokenSchema, token);

    HttpResponseMessage response = await client.PostAsJsonAsync(url, input);

    if(response.IsSuccessStatusCode)
    {
        TObject result = await response.Content.ReadAsAsync<TObject>();

        return result;
    }
    else
    {
        string finalURL = response.RequestMessage.RequestUri.AbsoluteUri;

        if(finalURL != url)
        {
            response = await client.PostAsJsonAsync(finalURL, input);

            if(response.IsSuccessStatusCode)
            {
                TObject result = await response.Content.ReadAsAsync<TObject>();

                return result;
            }
            else
            {
                return default;
            }
        }
        else
        {
            return default;
        }
    }
}

#endregion
#region POST 요청 객체 구하기 (비동기) - GetPOSTRequestObjectAsync<TObject>(url, token, input)

/// <summary>
/// POST 요청 객체 구하기 (비동기)
/// </summary>
/// <typeparam name="TObject">객체 타입</typeparam>
/// <param name="url">URL</param>
/// <param name="token">토큰</param>
/// <param name="input">입력 객체</param>
/// <returns>POST 요청 객체 태스크</returns>
public async Task<TObject> GetPOSTRequestObjectAsync<TObject>(string url, string token, object input)
{
    TObject result = await GetPOSTRequestObjectAsync<TObject>(url, "Bearer", token, input);

    return result;
}

#endregion

#region HTTP 클라이언트 구하기 - GetHTTPClient(useCookie, baseAddress, tokenShema, token)

/// <summary>
/// HTTP 클라이언트 구하기
/// </summary>
/// <param name="useCookie">쿠키 사용 여부</param>
/// <param name="baseAddress">기본 주소</param>
/// <param name="tokenShema">토큰 스키마</param>
/// <param name="token">토큰</param>
/// <returns>HTTP 클라이언트</returns>
private HttpClient GetHTTPClient(bool useCookie, string baseAddress, string tokenShema, string token)
{
    HttpClient client;

    if(useCookie)
    {
        HttpClientHandler httpClientHandler = new HttpClientHandler();

        httpClientHandler.UseCookies      = true;
        httpClientHandler.CookieContainer = new CookieContainer();
    
        client = new HttpClient(httpClientHandler);
    }
    else
    {
        client = new HttpClient();
    }

    client.DefaultRequestHeaders.Accept.Clear();

    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    if(!string.IsNullOrWhiteSpace(baseAddress))
    {
        client.BaseAddress = new Uri(baseAddress);
    }

    if(!string.IsNullOrWhiteSpace(tokenShema))
    {
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(tokenShema, token);
    }

    return client;
}

#endregion
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요