tx
2025-04-09 fa7510e1ed63df0366787fa4ed1b3db6426d2b46
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
 
namespace TProduct.HttpClient
{
    /// <summary>
    /// HTTP请求辅助类
    /// </summary>
    public class HttpRequestHelper
    {
        /// <summary>
        /// 获取
        /// </summary>
        public static string Get(string url, Dictionary<string, string> headers = null, Version version = null)
        {
            return Request(url, "GET", headers, null, version);
        }
 
        /// <summary>
        /// 提交
        /// </summary>
        public static string Post(string url, Dictionary<string, string> headers = null, string data = null, Version version = null)
        {
            return Request(url, "POST", headers, data, version);
        }
 
        /// <summary>
        /// 更新
        /// </summary>
        public static string Put(string url, Dictionary<string, string> headers = null, string data = null, Version version = null)
        {
            return Request(url, "PUT", headers, data, version);
        }
 
        /// <summary>
        /// 删除
        /// </summary>
        public static string Delete(string url, Dictionary<string, string> headers = null, string data = null, Version version = null)
        {
            return Request(url, "DELETE", headers, data, version);
        }
 
        //请求
        private static string Request(string url, string type, Dictionary<string, string> headers = null, string data = null, Version version = null)
        {
            var request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = type;
            request.ContentType = "application/json";
            if (version != null)
            { //默认是HttpVersion.Version11
                request.ProtocolVersion = version;
            }
 
            if (headers != null && headers.Count > 0)
            {
                foreach (var header in headers)
                {
                    request.Headers.Add(header.Key, header.Value);
                }
            }
 
            #region 注释
            //request.Timeout = 10 * 1000;//请求超时时间
            //if (!string.IsNullOrEmpty(data))
            //{
            //request.ContentLength = Encoding.UTF8.GetBytes(data).Length;
            //} 
            #endregion
 
            if (!string.IsNullOrEmpty(data))
            {
                using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                {
                    streamWriter.Write(data);
                    streamWriter.Flush();
                }
            }
 
            string responseText;
            using (var response = (HttpWebResponse)request.GetResponse())
            using (var reader = new StreamReader(response.GetResponseStream()))
            {
                responseText = reader.ReadToEnd();
            }
            return responseText; 
        }
    }
}