コード例 #1
0
 /// <summary>
 /// send a http request and receive response value
 /// </summary>
 /// <param name="option">request options</param>
 /// <returns></returns>
 public static async Task <HttpResponseMessage> HttpCallAsync(HttpRequestOption option)
 {
     if (option == null || option.RequestMessage == null)
     {
         throw new ArgumentException(nameof(option));
     }
     if (option.RequestMessage.Method == HttpMethod.Get)
     {
         return(await HttpGetAsync(option).ConfigureAwait(false));
     }
     if (option.RequestMessage.Method == HttpMethod.Post)
     {
         return(await HttpPostAsync(option).ConfigureAwait(false));
     }
     if (option.RequestMessage.Method == HttpMethod.Delete)
     {
         return(await HttpDeleteAsync(option).ConfigureAwait(false));
     }
     //if (option.RequestMessage.Method == HttpMethod.Patch)
     //{
     //    return await HttpPatchAsync(option).ConfigureAwait(false);
     //}
     if (option.RequestMessage.Method == HttpMethod.Put)
     {
         return(await HttpPutAsync(option).ConfigureAwait(false));
     }
     return(new HttpResponseMessage()
     {
         StatusCode = HttpStatusCode.BadRequest,
         Content = new StringContent("")
     });
 }
コード例 #2
0
        /// <summary>
        /// send a http request by Patch
        /// </summary>
        /// <param name="option"></param>
        /// <returns></returns>
        public static async Task <HttpResponseMessage> HttpPatchAsync(HttpRequestOption option)
        {
            if (option == null)
            {
                throw new ArgumentNullException(nameof(option));
            }
            using (var httpHandler = new HttpClientHandler()
            {
                UseCookies = option.UseCookie
            })
            {
                using (var httpClient = new HttpClient(httpHandler))
                {
                    httpClient.Timeout = TimeSpan.FromSeconds(option.TimeOutSeconds);
                    string url = option.RequestMessage?.RequestUri.AbsoluteUri;
                    if (string.IsNullOrWhiteSpace(url))
                    {
                        throw new ArgumentNullException("uri");
                    }
                    var formContentData = new MultipartFormDataContent();

                    #region files

                    if (option.RequestFiles != null && option.RequestFiles.Count > 0)
                    {
                        int fileCount = 0;
                        foreach (var item in option.RequestFiles)
                        {
                            if (item.Value == null || item.Value.Length <= 0)
                            {
                                continue;
                            }
                            HttpContent content = new StreamContent(new MemoryStream(item.Value));
                            formContentData.Add(content, "file" + fileCount.ToString(), item.Key);
                            fileCount++;
                        }
                    }

                    #endregion

                    #region parameters

                    if (option.RequestParameters != null && option.RequestParameters.Count > 0)
                    {
                        foreach (string key in option.RequestParameters.Keys)
                        {
                            var stringContent = new StringContent(option.RequestParameters[key]);
                            formContentData.Add(stringContent, key);
                        }
                    }

                    #endregion

                    return(await Task.FromResult(new HttpResponseMessage()
                    {
                        StatusCode = HttpStatusCode.BadGateway
                    })); //httpClient.PatchAsync(url, formContentData).ConfigureAwait(false);
                }
            }
        }
コード例 #3
0
        /// <summary>
        /// send a http request and get a string response value
        /// </summary>
        /// <param name="option">request options</param>
        /// <returns></returns>
        public static async Task <string> HttpResponseStringAsync(HttpRequestOption option)
        {
            var response = await HttpCallAsync(option).ConfigureAwait(false);

            using (var streamReader = new StreamReader(await response.Content.ReadAsStreamAsync()))
            {
                return(await streamReader.ReadToEndAsync().ConfigureAwait(false));
            }
        }
コード例 #4
0
        /// <summary>
        /// send a http request by Get
        /// </summary>
        /// <param name="option">option</param>
        /// <returns></returns>
        public static async Task <HttpResponseMessage> HttpGetAsync(HttpRequestOption option)
        {
            if (option == null)
            {
                throw new ArgumentNullException(nameof(option));
            }
            using (var httpHandler = new HttpClientHandler()
            {
                UseCookies = option.UseCookie
            })
            {
                using (var httpClient = new HttpClient(httpHandler))
                {
                    httpClient.Timeout = TimeSpan.FromSeconds(option.TimeOutSeconds);
                    string url = option.RequestMessage?.RequestUri.AbsoluteUri;
                    if (string.IsNullOrWhiteSpace(url))
                    {
                        throw new ArgumentNullException("uri");
                    }

                    #region parameters

                    if (option.RequestParameters != null || option.RequestParameters.Count > 0)
                    {
                        List <string> queryParameters = new List <string>();
                        queryParameters.AddRange(option.RequestParameters.Select(c => string.Format("{0}={1}", c.Key, c.Value)));
                        url  = url.Trim('?', '&');
                        url += (url.IndexOf('?') > 0 ? "&" : "?") + string.Join("&", queryParameters.ToArray());
                    }

                    #endregion

                    return(await httpClient.GetAsync(url).ConfigureAwait(false));
                }
            }
        }