示例#1
0
    /// <summary>
    /// 获取 HTTP 内容。
    /// </summary>
    /// <param name="endpointOptions">给定的 <see cref="HttpEndpointOptions"/>。</param>
    /// <returns>返回 <see cref="HttpContent"/>。</returns>
    protected virtual HttpContent?GetHttpContent(HttpEndpointOptions endpointOptions)
    {
        var parameters = endpointOptions.GetParameters();

        if (parameters is null)
        {
            return(null);
        }

        switch (endpointOptions.ContentType)
        {
        case HttpClientContentTypes.FormUrlEncoded:
        {
            _logger.LogInformation($"Use 'application/x-www-form-urlencoded' content type: {endpointOptions.Parameters}");
            return(new FormUrlEncodedContent(parameters));
        }

        case HttpClientContentTypes.MultipartFormData:
        {
            _logger.LogInformation($"Use 'multipart/form-data' content type: {endpointOptions.Parameters}");

            var content = new MultipartFormDataContent();

            foreach (var pair in parameters)
            {
                if (File.Exists(pair.Value))
                {
                    _logger.LogInformation($"Add file: {pair.Key}={pair.Value}");
                    content.Add(new StreamContent(File.Open(pair.Value, FileMode.Open)), pair.Key, pair.Value);
                }
                else
                {
                    _logger.LogInformation($"Add name: {pair.Key}={pair.Value}");
                    content.Add(new StringContent(pair.Value, Encoding), pair.Key);
                }
            }

            return(content);
        }

        case HttpClientContentTypes.Json:
        {
            _logger.LogInformation($"Use 'application/json' content type: {endpointOptions.Parameters}");

            var json = JsonSerializer.Serialize(parameters);
            return(new StringContent(json, Encoding));
        }

        case HttpClientContentTypes.Stream:
        {
            _logger.LogInformation($"Use 'binary' content type: {endpointOptions.Parameters}");
            return(new ByteArrayContent(endpointOptions.Parameters !.FromBase64String()));
        }

        default:
            goto case HttpClientContentTypes.Json;
        }
    }
示例#2
0
    /// <summary>
    /// 异步获取 HTTP 响应消息。
    /// </summary>
    /// <param name="client">给定的 <see cref="HttpClient"/>。</param>
    /// <param name="uri">给定的 <see cref="Uri"/> 请求。</param>
    /// <param name="endpointOptions">给定的 <see cref="HttpEndpointOptions"/>。</param>
    /// <param name="cancellationToken">给定的 <see cref="CancellationToken"/>。</param>
    /// <returns>返回包含 <see cref="HttpRequestMessage"/> 的异步操作。</returns>
    protected virtual Task <HttpResponseMessage> GetHttpResponseMessageAsync(HttpClient client, Uri uri,
                                                                             HttpEndpointOptions endpointOptions, CancellationToken cancellationToken)
    {
        Task <HttpResponseMessage> response;

        switch (endpointOptions.Method)
        {
        case HttpClientMethods.Get:
        {
            _logger.LogInformation($"Starting get the endpoint request: {uri}");
            response = client.GetAsync(uri, cancellationToken);
        }
        break;

        case HttpClientMethods.Delete:
        {
            _logger.LogInformation($"Starting delete the endpoint request: {uri}");
            response = client.DeleteAsync(uri, cancellationToken);
        }
        break;

        case HttpClientMethods.Post:
        {
            _logger.LogInformation($"Starting post the endpoint request: {uri}");
            response = client.PostAsync(uri, GetHttpContent(endpointOptions), cancellationToken);
        }
        break;

        case HttpClientMethods.Put:
        {
            _logger.LogInformation($"Starting post the endpoint request: {uri}");
            response = client.PutAsync(uri, GetHttpContent(endpointOptions), cancellationToken);
        }
        break;

        case HttpClientMethods.Patch:
        {
            _logger.LogInformation($"Starting post the endpoint request: {uri}");
            response = client.PatchAsync(uri, GetHttpContent(endpointOptions), cancellationToken);
        }
        break;

        default:
            goto case HttpClientMethods.Get;
        }

        if (!string.IsNullOrEmpty(endpointOptions.WaitTimeout))
        {
            _logger.LogInformation($"Set wait timeout: {endpointOptions.WaitTimeout}");
            response = response.WaitAsync(TimeSpan.Parse(endpointOptions.WaitTimeout), cancellationToken);
        }

        return(response);
    }