| 
									
										
										
										
											2021-04-30 10:28:37 +08:00
										 |  |  |  | using System; | 
					
						
							|  |  |  |  | using System.Collections; | 
					
						
							|  |  |  |  | using System.IO; | 
					
						
							|  |  |  |  | using System.Net; | 
					
						
							|  |  |  |  | using System.Text; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | namespace BLL | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     public static class APIGetHttpService | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         /// <summary> | 
					
						
							|  |  |  |  |         /// 不做catch处理,需要在外部做 | 
					
						
							|  |  |  |  |         /// </summary> | 
					
						
							|  |  |  |  |         /// <param name="url"></param> | 
					
						
							|  |  |  |  |         /// <param name="method">默认GET,空则补充为GET</param> | 
					
						
							|  |  |  |  |         /// <param name="contenttype">默认json,空则补充为json</param> | 
					
						
							|  |  |  |  |         /// <param name="header">请求头部</param> | 
					
						
							|  |  |  |  |         /// <param name="data">请求body内容</param> | 
					
						
							|  |  |  |  |         /// <returns></returns> | 
					
						
							|  |  |  |  |         public static string Http(string url, string method = "GET", string contenttype = "application/json;charset=utf-8", Hashtable header = null, string data = null) | 
					
						
							|  |  |  |  |         { | 
					
						
							| 
									
										
										
										
											2024-06-21 16:23:38 +08:00
										 |  |  |  |             ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | | 
					
						
							| 
									
										
										
										
											2025-09-13 13:08:54 +08:00
										 |  |  |  |             SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | | 
					
						
							|  |  |  |  |             SecurityProtocolType.Tls12; | 
					
						
							| 
									
										
										
										
											2021-04-30 10:28:37 +08:00
										 |  |  |  |             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); | 
					
						
							|  |  |  |  |             request.Method = string.IsNullOrEmpty(method) ? "GET" : method; | 
					
						
							|  |  |  |  |             request.ContentType = string.IsNullOrEmpty(contenttype) ? "application/json;charset=utf-8" : contenttype; | 
					
						
							|  |  |  |  |             if (header != null) | 
					
						
							|  |  |  |  |             { | 
					
						
							|  |  |  |  |                 foreach (var i in header.Keys) | 
					
						
							|  |  |  |  |                 { | 
					
						
							|  |  |  |  |                     request.Headers.Add(i.ToString(), header[i].ToString()); | 
					
						
							|  |  |  |  |                 } | 
					
						
							|  |  |  |  |             } | 
					
						
							| 
									
										
										
										
											2024-01-17 16:12:42 +08:00
										 |  |  |  |             else | 
					
						
							|  |  |  |  |             { | 
					
						
							|  |  |  |  |                 request.Headers.Add("token", "AF17168B-87BD-4GLY-1111-F0A0A1158F9B"); | 
					
						
							|  |  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-05-16 11:07:36 +08:00
										 |  |  |  |             request.Timeout = 20000; /// 设置5秒超时 | 
					
						
							| 
									
										
										
										
											2021-04-30 10:28:37 +08:00
										 |  |  |  |             if (!string.IsNullOrEmpty(data)) | 
					
						
							|  |  |  |  |             { | 
					
						
							|  |  |  |  |                 Stream RequestStream = request.GetRequestStream(); | 
					
						
							|  |  |  |  |                 byte[] bytes = Encoding.UTF8.GetBytes(data); | 
					
						
							|  |  |  |  |                 RequestStream.Write(bytes, 0, bytes.Length); | 
					
						
							|  |  |  |  |                 RequestStream.Close(); | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |             HttpWebResponse response = null; | 
					
						
							|  |  |  |  |             Stream ResponseStream = null; | 
					
						
							|  |  |  |  |             StreamReader StreamReader = null; | 
					
						
							|  |  |  |  |             try | 
					
						
							|  |  |  |  |             { | 
					
						
							|  |  |  |  |                 response = (HttpWebResponse)request.GetResponse(); | 
					
						
							|  |  |  |  |                 ResponseStream = response.GetResponseStream(); | 
					
						
							|  |  |  |  |                 StreamReader = new StreamReader(ResponseStream, Encoding.GetEncoding("utf-8")); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |                 string re = StreamReader.ReadToEnd(); | 
					
						
							|  |  |  |  |                 StreamReader.Close(); | 
					
						
							|  |  |  |  |                 ResponseStream.Close(); | 
					
						
							|  |  |  |  |                 return re; | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |             catch (WebException ex) | 
					
						
							|  |  |  |  |             { | 
					
						
							| 
									
										
										
										
											2025-09-02 10:48:28 +08:00
										 |  |  |  |                 string re = string.Empty; | 
					
						
							| 
									
										
										
										
											2021-04-30 10:28:37 +08:00
										 |  |  |  |                 response = (HttpWebResponse)ex.Response; | 
					
						
							| 
									
										
										
										
											2025-09-02 10:48:28 +08:00
										 |  |  |  |                 if (response != null) | 
					
						
							|  |  |  |  |                 { | 
					
						
							|  |  |  |  |                     ResponseStream = response.GetResponseStream(); | 
					
						
							|  |  |  |  |                     StreamReader = new StreamReader(ResponseStream, Encoding.GetEncoding("utf-8")); | 
					
						
							|  |  |  |  |                     re = StreamReader.ReadToEnd(); | 
					
						
							|  |  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-04-30 10:28:37 +08:00
										 |  |  |  |                 return re; | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |             finally | 
					
						
							|  |  |  |  |             { | 
					
						
							|  |  |  |  |                 if (StreamReader != null) | 
					
						
							|  |  |  |  |                 { | 
					
						
							|  |  |  |  |                     StreamReader.Close(); | 
					
						
							|  |  |  |  |                 } | 
					
						
							|  |  |  |  |                 if (ResponseStream != null) | 
					
						
							|  |  |  |  |                 { | 
					
						
							|  |  |  |  |                     ResponseStream.Close(); | 
					
						
							|  |  |  |  |                 } | 
					
						
							|  |  |  |  |                 if (response != null) | 
					
						
							|  |  |  |  |                 { | 
					
						
							|  |  |  |  |                     response.Close(); | 
					
						
							|  |  |  |  |                 } | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         /// <summary> | 
					
						
							|  |  |  |  |         /// 不做catch处理,需要在外部做 | 
					
						
							|  |  |  |  |         /// </summary> | 
					
						
							|  |  |  |  |         /// <param name="url"></param> | 
					
						
							|  |  |  |  |         /// <param name="method">默认GET,空则补充为GET</param> | 
					
						
							|  |  |  |  |         /// <param name="contenttype">默认json,空则补充为json</param> | 
					
						
							|  |  |  |  |         /// <param name="header">请求头部</param> | 
					
						
							|  |  |  |  |         /// <param name="data">请求body内容</param> | 
					
						
							|  |  |  |  |         /// <returns></returns> | 
					
						
							|  |  |  |  |         public static string OutsideHttp(string url, string method = "GET", string contenttype = "application/json;charset=utf-8", Hashtable header = null, string data = null) | 
					
						
							|  |  |  |  |         { | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); | 
					
						
							| 
									
										
										
										
											2021-10-19 17:50:34 +08:00
										 |  |  |  |             request.Method = string.IsNullOrEmpty(method) ? "GET" : method; | 
					
						
							| 
									
										
										
										
											2024-06-21 16:23:38 +08:00
										 |  |  |  |             request.ContentType = string.IsNullOrEmpty(contenttype) ? "application/json;charset=utf-8" : contenttype; | 
					
						
							| 
									
										
										
										
											2021-10-19 17:50:34 +08:00
										 |  |  |  |             if (header != null) | 
					
						
							|  |  |  |  |             { | 
					
						
							|  |  |  |  |                 foreach (var i in header.Keys) | 
					
						
							|  |  |  |  |                 { | 
					
						
							|  |  |  |  |                     request.Headers.Add(i.ToString(), header[i].ToString()); | 
					
						
							|  |  |  |  |                 } | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |             if (!string.IsNullOrEmpty(data)) | 
					
						
							|  |  |  |  |             { | 
					
						
							|  |  |  |  |                 Stream RequestStream = request.GetRequestStream(); | 
					
						
							|  |  |  |  |                 byte[] bytes = Encoding.UTF8.GetBytes(data); | 
					
						
							|  |  |  |  |                 RequestStream.Write(bytes, 0, bytes.Length); | 
					
						
							|  |  |  |  |                 RequestStream.Close(); | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |             HttpWebResponse response = null; | 
					
						
							|  |  |  |  |             Stream ResponseStream = null; | 
					
						
							|  |  |  |  |             StreamReader StreamReader = null; | 
					
						
							|  |  |  |  |             try | 
					
						
							|  |  |  |  |             { | 
					
						
							|  |  |  |  |                 response = (HttpWebResponse)request.GetResponse(); | 
					
						
							|  |  |  |  |                 ResponseStream = response.GetResponseStream(); | 
					
						
							|  |  |  |  |                 StreamReader = new StreamReader(ResponseStream, Encoding.GetEncoding("utf-8")); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |                 string re = StreamReader.ReadToEnd(); | 
					
						
							|  |  |  |  |                 return re; | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |             catch (WebException ex) | 
					
						
							|  |  |  |  |             { | 
					
						
							|  |  |  |  |                 response = (HttpWebResponse)ex.Response; | 
					
						
							|  |  |  |  |                 ResponseStream = response.GetResponseStream(); | 
					
						
							|  |  |  |  |                 StreamReader = new StreamReader(ResponseStream, Encoding.GetEncoding("utf-8")); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |                 string re = StreamReader.ReadToEnd(); | 
					
						
							|  |  |  |  |                 return re; | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |             finally | 
					
						
							|  |  |  |  |             { | 
					
						
							|  |  |  |  |                 if (StreamReader != null) | 
					
						
							|  |  |  |  |                 { | 
					
						
							|  |  |  |  |                     StreamReader.Close(); | 
					
						
							|  |  |  |  |                 } | 
					
						
							|  |  |  |  |                 if (ResponseStream != null) | 
					
						
							|  |  |  |  |                 { | 
					
						
							|  |  |  |  |                     ResponseStream.Close(); | 
					
						
							|  |  |  |  |                 } | 
					
						
							|  |  |  |  |                 if (response != null) | 
					
						
							|  |  |  |  |                 { | 
					
						
							|  |  |  |  |                     response.Close(); | 
					
						
							|  |  |  |  |                 } | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         /// <summary> | 
					
						
							|  |  |  |  |         /// 不做catch处理,需要在外部做 | 
					
						
							|  |  |  |  |         /// </summary> | 
					
						
							|  |  |  |  |         /// <param name="url"></param> | 
					
						
							|  |  |  |  |         /// <param name="method">默认GET,空则补充为GET</param> | 
					
						
							|  |  |  |  |         /// <param name="contenttype">默认json,空则补充为json</param> | 
					
						
							|  |  |  |  |         /// <param name="header">请求头部</param> | 
					
						
							|  |  |  |  |         /// <param name="data">请求body内容</param> | 
					
						
							|  |  |  |  |         /// <returns></returns> | 
					
						
							|  |  |  |  |         public static string ControlHttp(string url, string method = "GET", string contenttype = "application/json;charset=utf-8", Hashtable header = null, string data = null) | 
					
						
							|  |  |  |  |         { | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); | 
					
						
							| 
									
										
										
										
											2021-04-30 10:28:37 +08:00
										 |  |  |  |             request.Method = string.IsNullOrEmpty(method) ? "GET" : method; | 
					
						
							|  |  |  |  |             request.ContentType = string.IsNullOrEmpty(contenttype) ? "application/json;charset=utf-8" : contenttype; | 
					
						
							| 
									
										
										
										
											2022-05-16 11:07:36 +08:00
										 |  |  |  |             request.Timeout = 20000; /// 设置5秒超时 | 
					
						
							| 
									
										
										
										
											2021-04-30 10:28:37 +08:00
										 |  |  |  |             if (header != null) | 
					
						
							|  |  |  |  |             { | 
					
						
							|  |  |  |  |                 foreach (var i in header.Keys) | 
					
						
							|  |  |  |  |                 { | 
					
						
							|  |  |  |  |                     request.Headers.Add(i.ToString(), header[i].ToString()); | 
					
						
							|  |  |  |  |                 } | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |             if (!string.IsNullOrEmpty(data)) | 
					
						
							|  |  |  |  |             { | 
					
						
							|  |  |  |  |                 Stream RequestStream = request.GetRequestStream(); | 
					
						
							|  |  |  |  |                 byte[] bytes = Encoding.UTF8.GetBytes(data); | 
					
						
							|  |  |  |  |                 RequestStream.Write(bytes, 0, bytes.Length); | 
					
						
							|  |  |  |  |                 RequestStream.Close(); | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |             HttpWebResponse response = null; | 
					
						
							|  |  |  |  |             Stream ResponseStream = null; | 
					
						
							|  |  |  |  |             StreamReader StreamReader = null; | 
					
						
							|  |  |  |  |             try | 
					
						
							|  |  |  |  |             { | 
					
						
							|  |  |  |  |                 response = (HttpWebResponse)request.GetResponse(); | 
					
						
							|  |  |  |  |                 ResponseStream = response.GetResponseStream(); | 
					
						
							|  |  |  |  |                 StreamReader = new StreamReader(ResponseStream, Encoding.GetEncoding("utf-8")); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |                 string re = StreamReader.ReadToEnd(); | 
					
						
							|  |  |  |  |                 return re; | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |             catch (WebException ex) | 
					
						
							|  |  |  |  |             { | 
					
						
							|  |  |  |  |                 response = (HttpWebResponse)ex.Response; | 
					
						
							|  |  |  |  |                 ResponseStream = response.GetResponseStream(); | 
					
						
							|  |  |  |  |                 StreamReader = new StreamReader(ResponseStream, Encoding.GetEncoding("utf-8")); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |                 string re = StreamReader.ReadToEnd(); | 
					
						
							|  |  |  |  |                 return re; | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |             finally | 
					
						
							|  |  |  |  |             { | 
					
						
							|  |  |  |  |                 if (StreamReader != null) | 
					
						
							|  |  |  |  |                 { | 
					
						
							|  |  |  |  |                     StreamReader.Close(); | 
					
						
							|  |  |  |  |                 } | 
					
						
							|  |  |  |  |                 if (ResponseStream != null) | 
					
						
							|  |  |  |  |                 { | 
					
						
							|  |  |  |  |                     ResponseStream.Close(); | 
					
						
							|  |  |  |  |                 } | 
					
						
							|  |  |  |  |                 if (response != null) | 
					
						
							|  |  |  |  |                 { | 
					
						
							|  |  |  |  |                     response.Close(); | 
					
						
							|  |  |  |  |                 } | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | } |