zoukankan      html  css  js  c++  java
  • .NET Core Http请求(GET、POST、上传文件并携带参数)

    using Microsoft.Extensions.DependencyInjection;
    using System;
    using System.Collections.Generic;
    using System.Net.Http;
    using System.Net.Http.Headers;
    using System.Text;
    using System.Threading.Tasks;namespace Yanglao.Common.Helper
    {
        public class HttpHelper
        {
            private readonly IHttpClientFactory _httpClientFactory;
            public HttpHelper()
            {
                this._httpClientFactory = ServiceProviderHelper.ServiceProvider.GetRequiredService<IHttpClientFactory>();
            }
    
            public async Task<string> GetAsync(string url, Dictionary<string, string> headers = null, int timeoutSecond = 0)
            {
                var client = _httpClientFactory.CreateClient();
                var request = new HttpRequestMessage(HttpMethod.Get, url);
                if (headers != null)
                {
                    foreach (var header in headers)
                    {
                        request.Headers.Add(header.Key, header.Value);
                    }
                }
                if (timeoutSecond != 0)
                    client.Timeout = TimeSpan.FromSeconds(timeoutSecond);
                var response = await client.SendAsync(request);
                if (response.IsSuccessStatusCode)
                {
                    var result = await response.Content.ReadAsStringAsync();
                    return result;
                }
                else
                {
                    throw new HttpException($"HttpCode:{response.StatusCode},Message:{response.ReasonPhrase}");
                }
            }
    
            public async Task<string> PostAsync(string url, string requestString, HttpContentType contentType = HttpContentType.json, Dictionary<string, string> headers = null, int timeoutSecond = 0)
            {
                var client = _httpClientFactory.CreateClient();
                var requestContent = new StringContent(requestString, Encoding.UTF8, contentType.GetDescription());
                if (headers != null)
                {
                    foreach (var head in headers)
                    {
                        requestContent.Headers.Add(head.Key, head.Value);
                    }
                }
                if (timeoutSecond != 0)
                    client.Timeout = TimeSpan.FromSeconds(timeoutSecond);
                var response = await client.PostAsync(url, requestContent);
                if (response.IsSuccessStatusCode)
                {
                    var result = await response.Content.ReadAsStringAsync();
                    return result;
                }
                else
                {
                    throw new HttpException($"HttpCode:{response.StatusCode},Message:{response.ReasonPhrase}");
                }
            }
    
            /// <summary>
            /// 上传文件方法
            /// </summary>
            /// <param name="parameter">上传文件请求参数</param>
            public async Task<string> PostFileAsync(UploadParameterDto parameter, Dictionary<string, string> headers = null, int timeoutSecond = 0)
            {
                var client = _httpClientFactory.CreateClient();
                var content = new MultipartFormDataContent();
                var fileSteamConten = new StreamContent(parameter.UploadStream);
                fileSteamConten.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");
                content.Add(fileSteamConten, parameter.FileNameKey,parameter.FileNameValue);
                if (parameter.PostParameters != null && parameter.PostParameters.Count > 0)
                {
                    foreach (KeyValuePair<string, string> keyValuePair in parameter.PostParameters)
                    {
                        content.Add(new StringContent(keyValuePair.Value), keyValuePair.Key);
                    }
                }
    
                if(headers != null)
                {
                    foreach (var head in headers)
                    {
                        content.Headers.Add(head.Key, head.Value);
                    }
                }
                if (timeoutSecond != 0)
                    client.Timeout = TimeSpan.FromSeconds(timeoutSecond);
                //parameter.Url = "http://localhost:9329/api/External/LanidIdentityAuth";
                var response = await client.PostAsync(new Uri(parameter.Url), content);
                if (response.IsSuccessStatusCode)
                {
                    var result = await response.Content.ReadAsStringAsync();
                    return result;
                }
                else
                {
                    throw new HttpException($"HttpCode:{response.StatusCode},Message:{response.ReasonPhrase}");
                }
            }
    
            public async Task<string> PutAsync(string url, string requestString, HttpContentType contentType = HttpContentType.json, Dictionary<string, string> headers = null, int timeoutSecond = 0)
            {
                var client = _httpClientFactory.CreateClient();
                var requestContent = new StringContent(requestString, Encoding.UTF8, contentType.GetDescription());
                if (headers != null)
                {
                    foreach (var head in headers)
                    {
                        requestContent.Headers.Add(head.Key, head.Value);
                    }
                }
                if (timeoutSecond != 0)
                    client.Timeout = TimeSpan.FromSeconds(timeoutSecond);
                var response = await client.PutAsync(url, requestContent);
                if (response.IsSuccessStatusCode)
                {
                    var result = await response.Content.ReadAsStringAsync();
                    return result;
                }
                else
                {
                    throw new HttpException($"HttpCode:{response.StatusCode},Message:{response.ReasonPhrase}");
                }
            }
    
    
            public async Task<string> DeleteAsync(string url, Dictionary<string, string> headers = null, int timeoutSecond = 0)
            {
                var client = _httpClientFactory.CreateClient();
                var request = new HttpRequestMessage(HttpMethod.Delete, url);
                if (headers != null)
                {
                    foreach (var head in headers)
                    {
                        request.Headers.Add(head.Key, head.Value);
                    }
                }
                if (timeoutSecond != 0)
                    client.Timeout = TimeSpan.FromSeconds(timeoutSecond);
                var response = await client.SendAsync(request);
                if (response.IsSuccessStatusCode)
                {
                    var result = await response.Content.ReadAsStringAsync();
                    return result;
                }
                else
                {
                    throw new HttpException($"HttpCode:{response.StatusCode},Message:{response.ReasonPhrase}");
                }
            }
    
           
        }
    }
  • 相关阅读:
    oracle 7月份更新CVE-2020-14645 T3反序列化 Weblogic12.2.1.4.0 JNDI注入 Payload 复现&利用
    oracle 7月份更新 CVE-2020-14625 复现&利用
    Citrix Systems产品安全漏洞 CVE-2020-8193, CVE-2020-8195 and CVE-2020-8196 poc
    cve-2020-5902 RCE的payload以及绕过方式
    cve-2020-5902 BIG-IP RCE漏洞复现&exp
    Tomcat基于Servlet的无文件webshell的相关技术研究
    JBOSS 无文件webshell的技术研究
    weblogic 无文件webshell的技术研究
    java 获取包下的类 find all classes in a package
    冰蝎改造之适配基于tomcat Filter的无文件webshell
  • 原文地址:https://www.cnblogs.com/Mzg121584668/p/15565932.html
Copyright © 2011-2022 走看看