using Getf.Service.Transfer.Client.WinService.Helpers; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Collections.Specialized; using System.IO; using System.Linq; using System.Net; using System.Reflection; using System.Text; namespace Getf.Service.Transfer.Client.WinService.HttpHandler { public class HttpService { LogHelper _LogHelper; public HttpService() { _LogHelper = new LogHelper(); } public object DoRequest(string url, JObject jObject, out string msg) { try { HttpWebRequest httpWebRequest = WebRequest.Create(url) as HttpWebRequest; var head = jObject["Head"]; Encoding encoding = new UTF8Encoding(false); if (jObject["Encoding"] != null) { encoding = Encoding.GetEncoding(jObject["Encoding"].Value()); } if (jObject["ContentType"] != null) { httpWebRequest.ContentType = jObject["ContentType"].Value(); } var method = "POST"; if (jObject["Method"] != null) { method = jObject["Method"].Value(); } SetHead(httpWebRequest, method, head); object r; if (!"GET".Equals(method, StringComparison.OrdinalIgnoreCase)) { var requestStream = httpWebRequest.GetRequestStream(); SetRequestStream(httpWebRequest, requestStream, jObject, encoding); r = GetResponse(httpWebRequest, encoding); requestStream.Close(); requestStream.Dispose(); } else { r = GetResponse(httpWebRequest, encoding); } msg = String.Empty; return r; } catch (WebException e) { using (var stream = e?.Response?.GetResponseStream()) { msg = "http错误(" + (int)(e?.Response as HttpWebResponse)?.StatusCode + ")"; if (stream != null) { using (StreamReader streamReader = new StreamReader(stream)) { msg += streamReader.ReadToEnd(); _LogHelper.Info("请求错误原因为:" + msg); } } else { msg += e.Message; _LogHelper.Info("请求错误原因为:" + msg); } } } catch (Exception e) { msg = e.Message; _LogHelper.Info("请求错误原因为:" + msg); } return null; } private static PropertyInfo InnerCollectionProperty = typeof(WebHeaderCollection).GetProperty("InnerCollection", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); public static void SetHeaderValue(WebHeaderCollection header, string name, string value) { var collection = InnerCollectionProperty.GetValue(header, null) as NameValueCollection; collection[name] = value; } private void SetHead(HttpWebRequest httpWebRequest, string method, JToken head) { httpWebRequest.Method = method; if (head != null) { foreach (JProperty elem in head) { var name = elem.Name; var value = elem.Value.ToString(); SetHeaderValue(httpWebRequest.Headers, name, value); } } if ("post".Equals(method, StringComparison.OrdinalIgnoreCase)) { if (String.IsNullOrWhiteSpace(httpWebRequest.ContentType)) { httpWebRequest.ContentType = "application/x-www-form-urlencoded"; } } } private void SetRequestStream(HttpWebRequest httpWebRequest, Stream requestStream, JObject jObject, Encoding encoding) { using (var streamWrite = new StreamWriter(requestStream, encoding)) { var param = jObject["Param"]?.Value() ?? String.Empty; streamWrite.Write(param); } } private static readonly string[] TextContentTypes = new string[] { "application/json", "text/html" }; private object GetResponse(HttpWebRequest httpWebRequest, Encoding encoding) { var response = httpWebRequest.GetResponse(); using (var stream = response.GetResponseStream()) { if (IsTextContentType(response.ContentType)) { using (StreamReader streamReader = new StreamReader(stream, encoding)) { var r = streamReader.ReadToEnd(); return r; } } else { using (MemoryStream ms = new MemoryStream()) { stream.CopyTo(ms); return ms.ToArray(); } } } } private bool IsTextContentType(string contentType) { if (contentType == null) return true; contentType = contentType.ToLower(); var list = new string[] { "text/", "application/json", "application/xml" }; return list.Any(m => contentType.Contains(m)); } } }