ke_junjie
2025-06-04 84620534eb627e95811b971a4b552b6a177829bf
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
using Newtonsoft.Json;
using System;
using System.Web;
using System.IO;
using System.Net;
using System.Text;
using System.Net.Cache;
 
namespace iWareCommon.Utils
{
    public class HttpHelper
    {
        /// <summary>
        /// 获取response的静态方法
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="postData">请求参数</param>
        /// <param name="timeout">延迟时间</param>
        /// <returns>请求的response</returns>
        public static string GetHttpResponse(string url, object postData, string token, int timeout)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/json;charset=UTF-8";
            request.UserAgent = null;
            request.Timeout = timeout;
            request.Headers.Add("Authorization", token);
            var myRequestStream = request.GetRequestStream();
 
            var json = postData == null ? "" : JsonConvert.SerializeObject(postData);
 
            var jsonBytes = UTF8Encoding.UTF8.GetBytes(json);
            myRequestStream.Write(jsonBytes, 0, jsonBytes.Length);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();
            return retString;
        }
 
        /// <summary>
        /// 获取response的静态方法
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="postData">请求参数</param>
        /// <param name="timeout">延迟时间</param>
        /// <returns>请求的response</returns>
        public static T2 GetHttpResponse<T,T2>(string url, T postData,int timeout)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
            request.Method = "POST";
            request.ContentType = "application/json;charset=UTF-8";
            request.UserAgent = null;
            request.Timeout = timeout;
            //request.Headers.Add("Authorization", "eyJhbGciOiJIUzM4NCIsInR5cCI6IkpXVCJ9.eyJpYXQiOiIxNTUzNTMwNjAwIiwiZXhwIjoiMTYxNjYwMjYwMCIsImlzcyI6IlNBR1dXSVAiLCJzdWIiOiJ3Y3NpbnRlZ3JhdGVkIiwidXNlcm5hbWUiOiJ3Y3NpbnRlZ3JhdGVkIiwidXNlcmdyb3VwIjpbeyJsb2dpbm5hbWUiOiJ3Y3NpbnRlZ3JhdGVkIiwiZ3JvdXBJZCI6Ijg3YjE1MTQ1LWUwODMtNDM2Ny04ZjdiLTNiNTI3NWJhYzExZSIsImdyb3VwbmFtZSI6IlJDU2ludGVncmF0ZWQiLCJyb2xlSWQiOiI4NjNhMGIxZS0zODAzLTQyNmMtOTRlNC04OTRmNTE1ZWQyYzgiLCJyb2xlbmFtZSI6IlJDU-mbhuaIkCJ9LHsibG9naW5uYW1lIjoid2NzaW50ZWdyYXRlZCIsImdyb3VwSWQiOiI5ZGU3ZjA5Zi05YzhiLTQwNDEtODc0NS1hYjgxNGQ0OTFiODEiLCJncm91cG5hbWUiOiJXQ1NpbnRlZ3JhdGVkIiwicm9sZUlkIjoiNWJhZDE1YTUtOTQ1NC00YTc3LTk0OGYtNzUxMjI2NDdiOTA4Iiwicm9sZW5hbWUiOiJXQ1Ppm4bmiJAifV19.AN9iUPMFZ5w7UW7D5kERV903gPCtyk6o2STZILX7QzkVjKLLSWgpUnQYBBfiSSQk");
            var myRequestStream = request.GetRequestStream();
 
            var json = postData == null ? "" : JsonConvert.SerializeObject(postData);
 
            var jsonBytes = UTF8Encoding.UTF8.GetBytes(json);
            myRequestStream.Write(jsonBytes,0,jsonBytes.Length);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();
            myRequestStream.Close();
            T2 result = JsonConvert.DeserializeObject<T2>(retString);
            return result;
        }
 
        /// <summary>
        /// 获取response的静态方法
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="postData">请求参数</param>
        /// <param name="timeout">延迟时间</param>
        /// <param name="method">POST GET PUT DELETE</param>
        /// <returns>请求的response</returns>
        public static string GetHttpResponse(string url, object postData, int timeout,string method)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = method;
            request.ContentType = "application/json;charset=UTF-8";
            request.UserAgent = null;
            request.Timeout = timeout;
 
            if (!"GET".Equals(method))
            {
                var myRequestStream = request.GetRequestStream();
                var json = postData == null ? "" : JsonConvert.SerializeObject(postData);
                var jsonBytes = UTF8Encoding.UTF8.GetBytes(json);
                myRequestStream.Write(jsonBytes, 0, jsonBytes.Length);
            }
            
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();
 
            return retString;
        }
 
        /// <summary>
        /// Http上传文件
        /// </summary>
        /// <param name="url">上传的路径</param>
        /// <param name="path">本地文件路径</param>
        /// <returns>反馈的信息</returns>
        public static string HttpUploadFile(string url, string path)
        {
            // 设置参数
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            CookieContainer cookieContainer = new CookieContainer();
            request.CookieContainer = cookieContainer;
            request.AllowAutoRedirect = true;
            request.Method = "POST";
            string boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线
            request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary;
            byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");
            byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
            int pos = path.LastIndexOf("\\");
            string fileName = path.Substring(pos + 1);
            //请求头部信息 
            StringBuilder sbHeader = new StringBuilder(string.Format("Content-Disposition:form-data;name=\"file\";filename=\"{0}\"\r\nContent-Type:application/octet-stream\r\n\r\n", fileName));
            byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString());
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
            byte[] bArr = new byte[fs.Length];
            fs.Read(bArr, 0, bArr.Length);
            fs.Close();
            Stream postStream = request.GetRequestStream();
            postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
            postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
            postStream.Write(bArr, 0, bArr.Length);
            postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
            postStream.Close();
            //发送请求并获取相应回应数据
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            //直到request.GetResponse()程序才开始向目标网页发送Post请求
            Stream instream = response.GetResponseStream();
            StreamReader sr = new StreamReader(instream, Encoding.UTF8);
            //返回结果网页(html)代码
            string content = sr.ReadToEnd();
            return content;
        }
 
        /// <summary>
        /// Http上传文件
        /// </summary>
        /// <param name="url">上传的路径</param>
        /// <param name="path">本地文件路径</param>
        /// <returns>反馈的信息</returns>
        public static string HttpUploadFile(string url, object postData, string token, int timeout, string path)
        {
            // 设置参数
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            CookieContainer cookieContainer = new CookieContainer();
            request.CookieContainer = cookieContainer;
            request.AllowAutoRedirect = true;
            request.Method = "POST";
            string boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线
            request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary;
            request.Timeout = timeout;
            request.Headers.Add("Authorization", token);
            var myRequestStream = request.GetRequestStream();
            var json = postData == null ? "" : JsonConvert.SerializeObject(postData);
            var jsonBytes = UTF8Encoding.UTF8.GetBytes(json);
            myRequestStream.Write(jsonBytes, 0, jsonBytes.Length);
 
            byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");
            byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
            int pos = path.LastIndexOf("\\");
            string fileName = path.Substring(pos + 1);
            
            //请求头部信息 
            StringBuilder sbHeader = new StringBuilder(string.Format("Content-Disposition:form-data;name=\"file\";filename=\"{0}\"\r\nContent-Type:application/octet-stream\r\n\r\n", fileName));
            byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString());
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
            byte[] bArr = new byte[fs.Length];
            fs.Read(bArr, 0, bArr.Length);
            fs.Close();
            Stream postStream = request.GetRequestStream();
            postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
            postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
            postStream.Write(bArr, 0, bArr.Length);
            postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
            postStream.Close();
            
            //发送请求并获取相应回应数据
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            
            //直到request.GetResponse()程序才开始向目标网页发送Post请求
            Stream instream = response.GetResponseStream();
            StreamReader sr = new StreamReader(instream, Encoding.UTF8);
            
            //返回结果网页(html)代码
            string content = sr.ReadToEnd();
            return content;
        }
 
        /// <summary>
        /// Http上传文件
        /// </summary>
        /// <param name="url">上传的路径</param>
        /// <param name="path">本地文件路径</param>
        /// <returns>反馈的信息</returns>
        public static string HttpUploadFile(string url, HttpPostedFile file,  string token, int timeout)
        {
            // 设置参数
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            CookieContainer cookieContainer = new CookieContainer();
            request.CookieContainer = cookieContainer;
            request.AllowAutoRedirect = true;
            request.Method = "POST";
            string boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线
            request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary;
            request.Timeout = timeout;
            request.Headers.Add("Authorization", token);
           
            byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");
            byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
            
            //请求头部信息 
            StringBuilder sbHeader = new StringBuilder(string.Format("Content-Disposition:form-data;name=\"file\";filename=\"{0}\"\r\nContent-Type:application/octet-stream\r\n\r\n", file.FileName));
            byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString());
 
            
            int fileLen = file.ContentLength;
            byte[] bArray = new byte[fileLen];
            file.InputStream.Read(bArray, 0, fileLen);
            
            Stream postStream = request.GetRequestStream();
            
            
            
            postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
            postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
            postStream.Write(bArray, 0, bArray.Length);
            postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
            postStream.Close();
 
            //发送请求并获取相应回应数据
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
 
            //直到request.GetResponse()程序才开始向目标网页发送Post请求
            Stream instream = response.GetResponseStream();
            StreamReader sr = new StreamReader(instream, Encoding.UTF8);
 
            //返回结果网页(html)代码
            string content = sr.ReadToEnd();
            return content;
        }
    }
}