C# HttpClient请求

2021-04-22 14:27

阅读:474

标签:har   web   art   tps   pac   div   set   put   pre   

  1 using Newtonsoft.Json;
  2 using System;
  3 using System.Collections.Generic;
  4 using System.IO;
  5 using System.Linq;
  6 using System.Net;
  7 using System.Net.Http;
  8 using System.Net.Http.Headers;
  9 using System.Text;
 10 using System.Threading.Tasks;
 11 using System.Xml.Serialization;
 12 
 13 namespace SXYC.Common
 14 {
 15     public class HttpClientHelpClass
 16     {
 17         /// 
 18         /// get请求
 19         /// 
 20         /// 
 21         /// 
 22         public static string GetResponse(string url, out string statusCode)
 23         {
 24             if (url.StartsWith("https"))
 25                 System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
 26 
 27             var httpClient = new HttpClient();
 28             httpClient.DefaultRequestHeaders.Accept.Add(
 29               new MediaTypeWithQualityHeaderValue("application/json"));
 30             HttpResponseMessage response = httpClient.GetAsync(url).Result;
 31             statusCode = response.StatusCode.ToString();
 32             if (response.IsSuccessStatusCode)
 33             {
 34                 string result = response.Content.ReadAsStringAsync().Result;
 35                 return result;
 36             }
 37             return null;
 38         }
 39 
 40         public static string RestfulGet(string url)
 41         {
 42             HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
 43             // Get response
 44             using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
 45             {
 46                 // Get the response stream
 47                 StreamReader reader = new StreamReader(response.GetResponseStream());
 48                 // Console application output
 49                 return reader.ReadToEnd();
 50             }
 51         }
 52 
 53         public static T GetResponse(string url)
 54            where T : class, new()
 55         {
 56             if (url.StartsWith("https"))
 57                 System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
 58 
 59             var httpClient = new HttpClient();
 60             httpClient.DefaultRequestHeaders.Accept.Add(
 61                new MediaTypeWithQualityHeaderValue("application/json"));
 62             HttpResponseMessage response = httpClient.GetAsync(url).Result;
 63 
 64             T result = default(T);
 65 
 66             if (response.IsSuccessStatusCode)
 67             {
 68                 Taskstring> t = response.Content.ReadAsStringAsync();
 69                 string s = t.Result;
 70 
 71                 result = JsonConvert.DeserializeObject(s);
 72             }
 73             return result;
 74         }
 75 
 76         /// 
 77         /// post请求
 78         /// 
 79         /// 
 80         /// post数据
 81         /// 
 82         public static string PostResponse(string url, string postData, out string statusCode)
 83         {
 84             if (url.StartsWith("https"))
 85                 System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
 86 
 87             HttpContent httpContent = new StringContent(postData);
 88             httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
 89             httpContent.Headers.ContentType.CharSet = "utf-8";
 90 
 91             HttpClient httpClient = new HttpClient();
 92             //httpClient..setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
 93 
 94             HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
 95 
 96             statusCode = response.StatusCode.ToString();
 97             if (response.IsSuccessStatusCode)
 98             {
 99                 string result = response.Content.ReadAsStringAsync().Result;
100                 return result;
101             }
102 
103             return null;
104         }
105 
106         /// 
107         /// 发起post请求
108         /// 
109         /// 
110         /// url
111         /// post数据
112         /// 
113         public static T PostResponse(string url, string postData)
114             where T : class, new()
115         {
116             if (url.StartsWith("https"))
117                 System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
118 
119             HttpContent httpContent = new StringContent(postData);
120             httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
121             HttpClient httpClient = new HttpClient();
122 
123             T result = default(T);
124 
125             HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
126 
127             if (response.IsSuccessStatusCode)
128             {
129                 Taskstring> t = response.Content.ReadAsStringAsync();
130                 string s = t.Result;
131 
132                 result = JsonConvert.DeserializeObject(s);
133             }
134             return result;
135         }
136 
137 
138         /// 
139         /// 反序列化Xml
140         /// 
141         /// 
142         /// 
143         /// 
144         public static T XmlDeserialize(string xmlString)
145             where T : class, new()
146         {
147             try
148             {
149                 XmlSerializer ser = new XmlSerializer(typeof(T));
150                 using (StringReader reader = new StringReader(xmlString))
151                 {
152                     return (T)ser.Deserialize(reader);
153                 }
154             }
155             catch (Exception ex)
156             {
157                 throw new Exception("XmlDeserialize发生异常:xmlString:" + xmlString + "异常信息:" + ex.Message);
158             }
159 
160         }
161 
162         public static string PostResponse(string url, string postData, string token, string appId, string serviceURL, out string statusCode)
163         {
164             if (url.StartsWith("https"))
165                 System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
166 
167             HttpContent httpContent = new StringContent(postData);
168             httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
169             httpContent.Headers.ContentType.CharSet = "utf-8";
170 
171             httpContent.Headers.Add("token", token);
172             httpContent.Headers.Add("appId", appId);
173             httpContent.Headers.Add("serviceURL", serviceURL);
174 
175 
176             HttpClient httpClient = new HttpClient();
177             //httpClient..setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
178 
179             HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
180 
181             statusCode = response.StatusCode.ToString();
182             if (response.IsSuccessStatusCode)
183             {
184                 string result = response.Content.ReadAsStringAsync().Result;
185                 return result;
186             }
187 
188             return null;
189         }
190 
191         /// 
192         /// 修改API
193         /// 
194         /// 
195         /// 
196         /// 
197         public static string KongPatchResponse(string url, string postData)
198         {
199             var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
200             httpWebRequest.ContentType = "application/x-www-form-urlencoded";
201             httpWebRequest.Method = "PATCH";
202 
203             byte[] btBodys = Encoding.UTF8.GetBytes(postData);
204             httpWebRequest.ContentLength = btBodys.Length;
205             httpWebRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length);
206 
207             HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
208             var streamReader = new StreamReader(httpWebResponse.GetResponseStream());
209             string responseContent = streamReader.ReadToEnd();
210 
211             httpWebResponse.Close();
212             streamReader.Close();
213             httpWebRequest.Abort();
214             httpWebResponse.Close();
215 
216             return responseContent;
217         }
218 
219         /// 
220         /// 创建API
221         /// 
222         /// 
223         /// 
224         /// 
225         public static string KongAddResponse(string url, string postData)
226         {
227             if (url.StartsWith("https"))
228                 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
229             HttpContent httpContent = new StringContent(postData);
230             httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded") { CharSet = "utf-8" };
231             var httpClient = new HttpClient();
232             HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
233             if (response.IsSuccessStatusCode)
234             {
235                 string result = response.Content.ReadAsStringAsync().Result;
236                 return result;
237             }
238             return null;
239         }
240 
241         /// 
242         /// 删除API
243         /// 
244         /// 
245         /// 
246         public static bool KongDeleteResponse(string url)
247         {
248             if (url.StartsWith("https"))
249                 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
250 
251             var httpClient = new HttpClient();
252             HttpResponseMessage response = httpClient.DeleteAsync(url).Result;
253             return response.IsSuccessStatusCode;
254         }
255 
256         /// 
257         /// 修改或者更改API?    ?    
258         /// 
259         /// 
260         /// 
261         /// 
262         public static string KongPutResponse(string url, string postData)
263         {
264             if (url.StartsWith("https"))
265                 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
266 
267             HttpContent httpContent = new StringContent(postData);
268             httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded") { CharSet = "utf-8" };
269 
270             var httpClient = new HttpClient();
271             HttpResponseMessage response = httpClient.PutAsync(url, httpContent).Result;
272             if (response.IsSuccessStatusCode)
273             {
274                 string result = response.Content.ReadAsStringAsync().Result;
275                 return result;
276             }
277             return null;
278         }
279 
280         /// 
281         /// 检索API
282         /// 
283         /// 
284         /// 
285         public static string KongSerchResponse(string url)
286         {
287             if (url.StartsWith("https"))
288                 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
289 
290             var httpClient = new HttpClient();
291             HttpResponseMessage response = httpClient.GetAsync(url).Result;
292             if (response.IsSuccessStatusCode)
293             {
294                 string result = response.Content.ReadAsStringAsync().Result;
295                 return result;
296             }
297             return null;
298         }
299     }
300 }

 

C# HttpClient请求

标签:har   web   art   tps   pac   div   set   put   pre   

原文地址:http://www.cnblogs.com/louby/p/8021527.html


评论


亲,登录后才可以留言!