C# HttpClientHelper

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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
public class HttpClientHelper
   {
 
       /// <summary>
       /// get请求
       /// </summary>
       /// <param name="url"></param>
       /// <returns></returns>
       public static string GetResponse(string url, out string statusCode)
       {
           if (url.StartsWith("https")) { System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; }
 
 
           using (var httpClient = new HttpClient())
           {
               httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
               using (HttpResponseMessage response = httpClient.GetAsync(url).Result)
               {
                   statusCode = response.StatusCode.ToString();
                   if (response.IsSuccessStatusCode)
                   {
                       string result = response.Content.ReadAsStringAsync().Result;
                       return result;
                   }
                   return null;
               }
           }
       }
       public static string RestfulGet(string url)
       {
           HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
           // Get response
           using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
           {
               // Get the response stream
               StreamReader reader = new StreamReader(response.GetResponseStream());
               // Console application output
               return reader.ReadToEnd();
           }
       }
       public static T GetResponse<T>(string url)where T : class, new()
       {
           if (url.StartsWith("https")) { System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; }
           using (var httpClient = new HttpClient())
           {
               httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
               using (HttpResponseMessage response = httpClient.GetAsync(url).Result)
               {
                   T result = default(T);
                   if (response.IsSuccessStatusCode)
                   {
                       Task<string> t = response.Content.ReadAsStringAsync();
                       string s = t.Result;
                       result = JsonConvert.DeserializeObject<T>(s);
                   }
                   return result;
               }
           }
       }
       /// <summary>
       /// GET请求
       /// </summary>
       /// <param name="url"></param>
       /// <returns></returns>
       public static string GetResponse(string url)
       {
           if (url.StartsWith("https"))
           { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; }
 
           using (HttpClient httpClient = new HttpClient())
           {
               using (HttpResponseMessage response = httpClient.GetAsync(url).Result)
               {
                   if (response.IsSuccessStatusCode)
                   {
                       string result = response.Content.ReadAsStringAsync().Result;
                       return result;
                   }
                   return null;
               }
           }
       }
 
 
       /// <summary>
       /// post请求
       /// </summary>
       /// <param name="url"></param>
       /// <param name="postData">post数据</param>
       /// <returns></returns>
       public static string PostResponse(string url, string postData, out string statusCode)
       {
           if (url.StartsWith("https")) { System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; }
           using (HttpContent httpContent = new StringContent(postData))
           {
               httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
               httpContent.Headers.ContentType.CharSet = "utf-8";
               using (HttpClient httpClient = new HttpClient())
               {
                   using (HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result)
                   {
                       statusCode = response.StatusCode.ToString();
                       if (response.IsSuccessStatusCode)
                       {
                           string result = response.Content.ReadAsStringAsync().Result;
                           return result;
                       }
                       return null;
                   }
               }
           }
       }
 
       /// <summary>
       /// 发起post请求
       /// </summary>
       /// <typeparam name="T"></typeparam>
       /// <param name="url">url</param>
       /// <param name="postData">post数据</param>
       /// <returns></returns>
       public static T PostResponse<T>(string url, string postData)where T : class, new()
       {
           if (url.StartsWith("https")) { System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; }
           using (HttpContent httpContent = new StringContent(postData))
           {
               httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
               using (HttpClient httpClient = new HttpClient())
               {
                   T result = default(T);
                   using (HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result)
                   {
                       if (response.IsSuccessStatusCode)
                       {
                           Task<string> t = response.Content.ReadAsStringAsync();
                           string s = t.Result;
 
                           result = JsonConvert.DeserializeObject<T>(s);
                       }
                       return result;
                   }
               }
           }
       }
 
 
 
 
       public static string PostResponse(string url, string postData, string token, string appId, string serviceURL, out string statusCode)
       {
           if (url.StartsWith("https")) { System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; }
 
 
           using (HttpContent httpContent = new StringContent(postData))
           {
               httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
               httpContent.Headers.ContentType.CharSet = "utf-8";
               httpContent.Headers.Add("token", token);
               httpContent.Headers.Add("appId", appId);
               httpContent.Headers.Add("serviceURL", serviceURL);
               using (HttpClient httpClient = new HttpClient())
               {
                   using (HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result)
                   {
                       statusCode = response.StatusCode.ToString();
                       if (response.IsSuccessStatusCode)
                       {
                           string result = response.Content.ReadAsStringAsync().Result;
                           return result;
                       }
                       return null;
                   }
               }
           }
       }
 
       /// <summary>
       /// 修改请求
       /// </summary>
       /// <param name="url"></param>
       /// <param name="postData"></param>
       /// <returns></returns>
       public static string PatchResponse(string url, string postData)
       {
 
           HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
           httpWebRequest.ContentType = "application/x-www-form-urlencoded";
           httpWebRequest.Method = "PATCH";
 
           byte[] btBodys = Encoding.UTF8.GetBytes(postData);
           httpWebRequest.ContentLength = btBodys.Length;
           httpWebRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length);
 
           using (HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse())
           {
               using (StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream()))
               {
                   string responseContent = streamReader.ReadToEnd();
                   httpWebResponse.Close();
                   streamReader.Close();
                   httpWebRequest.Abort();
                   httpWebResponse.Close();
                   return responseContent;
               }
           }
       }
 
       /// <summary>
       /// 创建Form表单API请求
       /// </summary>
       /// <param name="url"></param>
       /// <param name="postData"></param>
       /// <returns></returns>
       public static string CreateFormResponse(string url, string postData)
       {
           if (url.StartsWith("https")) { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; }
 
           using (HttpContent httpContent = new StringContent(postData))
           {
               httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded") { CharSet = "utf-8" };
               using (HttpClient httpClient = new HttpClient())
               {
                   using (HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result)
                   {
                       if (response.IsSuccessStatusCode)
                       {
                           string result = response.Content.ReadAsStringAsync().Result;
                           return result;
                       }
                       return null;
                   }
               }
           }
                
       }
 
       /// <summary>
       /// 删除API请求
       /// </summary>
       /// <param name="url"></param>
       /// <returns></returns>
       public static bool DeleteResponse(string url)
       {
           if (url.StartsWith("https")) { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; }
           using (HttpClient httpClient = new HttpClient())
           {
               using (HttpResponseMessage response = httpClient.DeleteAsync(url).Result)
               {
                   return response.IsSuccessStatusCode;
               }
           }  
       }
 
       /// <summary>
       /// 修改或者更改API    
       /// </summary>
       /// <param name="url"></param>
       /// <param name="postData"></param>
       /// <returns></returns>
       public static string PutResponse(string url, string postData)
       {
           if (url.StartsWith("https")) { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; }
 
 
           using (HttpContent httpContent = new StringContent(postData))
           {
               httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded") { CharSet = "utf-8" };
               using (HttpClient httpClient = new HttpClient())
               {
                   using (HttpResponseMessage response = httpClient.PutAsync(url, httpContent).Result)
                   {
                       if (response.IsSuccessStatusCode)
                       {
                           string result = response.Content.ReadAsStringAsync().Result;
                           return result;
                       }
                       return null;
                   }
               }
           }
       }
 
       
 
 
 
   }

 

posted @   LuoCore  阅读(691)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示