Util.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.IO;
  6. using Newtonsoft.Json.Linq;
  7. using Newtonsoft.Json;
  8. using SupplierWeb.Service;
  9. using System.Reflection;
  10. namespace SupplierWeb.Commonss
  11. {
  12. public static class Util
  13. {
  14. /// <summary>
  15. /// 把Model转换为DataRow
  16. /// </summary>
  17. /// <typeparam name="T"></typeparam>
  18. /// <param name="mod"></param>
  19. /// <returns></returns>
  20. public static T ParseDictionaryToModel<T>(Dictionary<string, object> dict)
  21. {
  22. //
  23. T obj = default(T);
  24. obj = Activator.CreateInstance<T>();
  25. //根据Key值设定 Columns
  26. foreach (KeyValuePair<string, object> item in dict)
  27. {
  28. PropertyInfo prop = obj.GetType().GetProperty(item.Key);
  29. if(null != prop && null != (item.Value))
  30. {
  31. object value = item.Value;
  32. //Nullable 获取Model类字段的真实类型
  33. Type itemType = Nullable.GetUnderlyingType(prop.PropertyType) == null ? prop.PropertyType : Nullable.GetUnderlyingType(prop.PropertyType);
  34. //根据Model类字段的真实类型进行转换
  35. prop.SetValue(obj, Convert.ChangeType(value, itemType), null);
  36. }
  37. }
  38. return obj;
  39. }
  40. static string HOST = "http://192.168.50.41:9200/";
  41. public static string ElasticCall(string url, string method, string query)
  42. {
  43. HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(HOST + url);
  44. myReq.Method = method;
  45. var headers = new WebHeaderCollection();
  46. headers.Add("Content-Type", "application/json");
  47. myReq.Headers = headers;
  48. if (method == "POST")
  49. {
  50. using (var streamWriter = new StreamWriter(myReq.GetRequestStream()))
  51. {
  52. string json = query;
  53. streamWriter.Write(json);
  54. streamWriter.Flush();
  55. streamWriter.Close();
  56. }
  57. }
  58. HttpWebResponse myResp = (HttpWebResponse)myReq.GetResponse();
  59. System.IO.StreamReader respStreamReader = new System.IO.StreamReader(myResp.GetResponseStream());
  60. string responseString = respStreamReader.ReadToEnd();
  61. respStreamReader.Close();
  62. myResp.Close();
  63. return responseString;
  64. }
  65. public static string ElasticQuery(string url, string method, string query)
  66. {
  67. HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(HOST + url);
  68. myReq.Method = method;
  69. var headers = new WebHeaderCollection();
  70. headers.Add("Content-Type", "application/json");
  71. myReq.Headers = headers;
  72. if (method == "POST")
  73. {
  74. using (var streamWriter = new StreamWriter(myReq.GetRequestStream()))
  75. {
  76. string json = query;
  77. streamWriter.Write(json);
  78. streamWriter.Flush();
  79. streamWriter.Close();
  80. }
  81. }
  82. HttpWebResponse myResp = (HttpWebResponse)myReq.GetResponse();
  83. System.IO.StreamReader respStreamReader = new System.IO.StreamReader(myResp.GetResponseStream());
  84. string responseString = respStreamReader.ReadToEnd();
  85. respStreamReader.Close();
  86. myResp.Close();
  87. return responseString;
  88. }
  89. public static JObject SqlQuery(string query)
  90. {
  91. var json = "{\"query\":\"" + query +
  92. "\"}";
  93. var str = ElasticCall( "_sql?format=json", "POST", json);
  94. var result = (JObject)JsonConvert.DeserializeObject(str);
  95. return result;
  96. }
  97. public static string WechatHttp(string url, string method, string query)
  98. {
  99. HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
  100. myReq.Method = method;
  101. var headers = new WebHeaderCollection();
  102. headers.Add("Content-Type", "application/json");
  103. myReq.Headers = headers;
  104. if (method == "POST")
  105. {
  106. using (var streamWriter = new StreamWriter(myReq.GetRequestStream()))
  107. {
  108. string json = query;
  109. streamWriter.Write(json);
  110. streamWriter.Flush();
  111. streamWriter.Close();
  112. }
  113. }
  114. HttpWebResponse myResp = (HttpWebResponse)myReq.GetResponse();
  115. System.IO.StreamReader respStreamReader = new System.IO.StreamReader(myResp.GetResponseStream());
  116. string responseString = respStreamReader.ReadToEnd();
  117. respStreamReader.Close();
  118. myResp.Close();
  119. return responseString;
  120. }
  121. }
  122. }