using System; using System.Collections.Generic; using System.Data; using System.IO; using System.IO.Compression; using System.Linq; using System.Net; using System.Text; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace JobPingTaiWork { public static class DataTableToJsonHelper { public static string ConvertToString(DataTable table) { StringWriter writer = new StringWriter(); JsonWriter jsonWriter = new JsonTextWriter(writer); jsonWriter.WriteStartArray(); foreach (DataRow row in table.Rows) { jsonWriter.WriteStartObject(); foreach (DataColumn col in table.Columns) { jsonWriter.WritePropertyName(col.ColumnName); jsonWriter.WriteValue(row[col.ColumnName]); } jsonWriter.WriteEndObject(); } jsonWriter.WriteEndArray(); return writer.ToString(); } public static JArray ConvertToJson(DataTable table) { JArray jarr = new JArray(); foreach (DataRow row in table.Rows) { JObject jobj = new JObject(); foreach (DataColumn col in table.Columns) { jobj.Add(col.ColumnName, new JValue(row[col.ColumnName])); } jarr.Add(jobj); } return jarr; } } public static class WebRequestHelper { public static string DoGet(string url) { string responseString = "";//post返回的结果 ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, err) => { return true; }; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url); req.Method = "GET"; req.ContentLength = 0; var response = req.GetResponse(); Stream streamResponse = response.GetResponseStream(); StreamReader streamRead = new StreamReader(streamResponse); responseString = streamRead.ReadToEnd(); response.Close(); streamRead.Close(); return responseString; } public static string DoPost(string url, string json, int timeout = 10000) { string responseString = "";//post返回的结果 ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, err) => { return true; }; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url); req.Method = "POST"; req.Timeout = timeout; req.Headers.Add("Accept-Encoding", "gzip, deflate"); if (!string.IsNullOrWhiteSpace(json)) { byte[] postBytes = Encoding.UTF8.GetBytes(json); req.ContentType = "application/json; charset=utf-8"; req.ContentLength = Encoding.UTF8.GetByteCount(json); Stream stream = req.GetRequestStream(); stream.Write(postBytes, 0, postBytes.Length); stream.Close(); } else { req.ContentLength = 0; } var response = req.GetResponse(); responseString = GetResponseBody((HttpWebResponse)response); return responseString; } public static string DoPost(string url, Dictionary dic, int timeout = 10000) { string responseString = "";//post返回的结果 ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, err) => { return true; }; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url); req.Method = "POST"; req.Timeout = timeout; req.Headers.Add("Accept-Encoding", "gzip, deflate"); if (dic.Count > 0) { string strContent = string.Empty; foreach (var item in dic) { if (strContent.Length > 0) { strContent += "&"; } strContent += $"{item.Key}={item.Value}"; } byte[] postBytes = Encoding.UTF8.GetBytes(strContent); req.ContentType = "application/x-www-form-urlencoded"; req.ContentLength = postBytes.Length; Stream stream = req.GetRequestStream(); stream.Write(postBytes, 0, postBytes.Length); stream.Close(); } else { req.ContentLength = 0; } var response = req.GetResponse(); responseString = GetResponseBody((HttpWebResponse)response); return responseString; } private static string GetResponseBody(HttpWebResponse response) { string responseBody = string.Empty; if (response.ContentEncoding.ToLower().Contains("gzip")) { using (GZipStream stream = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress)) { using (StreamReader reader = new StreamReader(stream)) { responseBody = reader.ReadToEnd(); } } } else if (response.ContentEncoding.ToLower().Contains("deflate")) { using (DeflateStream stream = new DeflateStream( response.GetResponseStream(), CompressionMode.Decompress)) { using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { responseBody = reader.ReadToEnd(); } } } else { using (Stream stream = response.GetResponseStream()) { using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { responseBody = reader.ReadToEnd(); } } } return responseBody; } } }