username@email.com
2024-09-09 626943b5ba84ce44bc19f4c3b8e8e94638bec733
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/***********************************************************************
 *            Project: baifenBinfa
 *        ProjectName: 百分兵法管理系统                               
 *                Web: http://chuanyin.com                     
 *             Author:                                        
 *              Email:                               
 *         CreateTime: 2021/7/29 23:24:45
 *        Description: 暂无
 ***********************************************************************/
 
 
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
 
namespace CoreCms.Net.WeChat.Service.Utilities
{
    /// <summary>HTTP 请求工具类</summary>
    public static class RequestUtility
    {
        /// <summary>【异步方法】从 Request.Body 中读取流,并复制到一个独立的 MemoryStream 对象中</summary>
        /// <param name="request"></param>
        /// <param name="allowSynchronousIO"></param>
        /// <returns></returns>
        public static async Task<Stream> GetRequestMemoryStreamAsync(
          this HttpRequest request,
          bool? allowSynchronousIO = true)
        {
            IHttpBodyControlFeature bodyControlFeature = request.HttpContext.Features.Get<IHttpBodyControlFeature>();
            if (bodyControlFeature != null && allowSynchronousIO.HasValue)
                bodyControlFeature.AllowSynchronousIO = allowSynchronousIO.Value;
            return (Stream)new MemoryStream(Encoding.UTF8.GetBytes(await new StreamReader(request.Body).ReadToEndAsync()));
        }
 
        /// <summary>从 Request.Body 中读取流,并复制到一个独立的 MemoryStream 对象中</summary>
        /// <param name="request"></param>
        /// <param name="allowSynchronousIO"></param>
        /// <returns></returns>
        public static Stream GetRequestStream(
            this HttpRequest request,
            bool? allowSynchronousIO = true)
        {
            IHttpBodyControlFeature bodyControlFeature = request.HttpContext.Features.Get<IHttpBodyControlFeature>();
            if (bodyControlFeature != null && allowSynchronousIO.HasValue)
                bodyControlFeature.AllowSynchronousIO = allowSynchronousIO.Value;
            return (Stream)new MemoryStream(Encoding.UTF8.GetBytes(new StreamReader(request.Body).ReadToEnd()));
        }
 
        /// <summary>从 Request.Body 中读取流,并复制到一个独立的 MemoryStream 对象中</summary>
        /// <param name="request"></param>
        /// <param name="allowSynchronousIO"></param>
        /// <returns></returns>
        public static MemoryStream GetRequestMemoryStream(
            this HttpRequest request,
            bool? allowSynchronousIO = true)
        {
            IHttpBodyControlFeature bodyControlFeature = request.HttpContext.Features.Get<IHttpBodyControlFeature>();
            if (bodyControlFeature != null && allowSynchronousIO.HasValue)
                bodyControlFeature.AllowSynchronousIO = allowSynchronousIO.Value;
            return new MemoryStream(Encoding.UTF8.GetBytes(new StreamReader(request.Body).ReadToEnd()));
        }
 
 
    }
 
}