You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
6.9 KiB

3 weeks ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Reflection;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace NFine.Data.Extensions
  11. {
  12. public class PublicMethod
  13. {
  14. /// <summary>
  15. /// 获取单据号
  16. /// </summary>
  17. /// <param name="tbName"></param>
  18. /// <param name="colName"></param>
  19. /// <param name="Pre"></param>
  20. /// <param name="numLen"></param>
  21. /// <returns></returns>
  22. public static string GetSerialCode( string tbName, string colName, string Pre, int numLen)
  23. {
  24. string sql = "EXEC Addins_GetSerialCode '0001','{0}','{1}','{2}',{3}";
  25. sql = string.Format(sql, new object[] { tbName, colName, Pre, numLen });
  26. return SqlHelper.ExecuteScalar(sql).ToString();
  27. }
  28. ///<summary>
  29. ///copy相同字段的数据
  30. public static D Mapper<D, S>(S s)
  31. {
  32. D d = Activator.CreateInstance<D>();
  33. try
  34. {
  35. var Types = s.GetType();//获得类型
  36. var Typed = typeof(D);
  37. foreach (PropertyInfo sp in Types.GetProperties())//获得类型的属性字段
  38. {
  39. foreach (PropertyInfo dp in Typed.GetProperties())
  40. {
  41. if (dp.Name == sp.Name)//判断属性名是否相同
  42. {
  43. dp.SetValue(d, sp.GetValue(s, null), null);//获得s对象属性的值复制给d对象的属性
  44. }
  45. }
  46. }
  47. }
  48. catch (Exception ex)
  49. {
  50. throw ex;
  51. }
  52. return d;
  53. }
  54. /// <summary>
  55. /// Get接口查询方法
  56. /// </summary>
  57. /// <param name="apiName"></param>
  58. /// <param name="url"></param>
  59. /// <returns></returns>
  60. public static string QueryPostparamsService(string apiName, string url)
  61. {
  62. string result = "";
  63. HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
  64. req.Method = "Get";
  65. //req.Headers.Add()
  66. try
  67. {
  68. HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
  69. Stream stream = resp.GetResponseStream();
  70. //获取内容
  71. using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
  72. {
  73. result = reader.ReadToEnd();
  74. }
  75. }
  76. catch (Exception ex)
  77. {
  78. throw new Exception(apiName + "调用失败," + ex.Message);
  79. }
  80. return result;
  81. }
  82. /// <summary>
  83. ///
  84. /// </summary>
  85. /// <param name="apiName"></param>
  86. /// <param name="url"></param>
  87. /// <param name="body"></param>
  88. /// <returns></returns>
  89. public static string HttpPost(string apiName, string url, string body)
  90. {
  91. try
  92. {
  93. Encoding encoding = Encoding.UTF8;
  94. System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
  95. request.Method = "POST";
  96. if (url.StartsWith("https://", StringComparison.InvariantCultureIgnoreCase))
  97. {
  98. request.ProtocolVersion = HttpVersion.Version11;
  99. request.ServerCertificateValidationCallback = (a, b, c, d) => true;
  100. }
  101. //log.Debug(url + Environment.NewLine + body);
  102. request.Accept = "application/json, text/javascript, */*"; //"text/html, application/xhtml+xml, */*";
  103. request.ContentType = "application/json; charset=utf-8";
  104. // request.ContentType = "text/html, application/xhtml+xml";
  105. byte[] buffer = encoding.GetBytes(body);
  106. request.ContentLength = buffer.Length;
  107. request.GetRequestStream().Write(buffer, 0, buffer.Length);
  108. System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
  109. using (System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream(), encoding))
  110. {
  111. return reader.ReadToEnd();
  112. }
  113. }
  114. catch (System.Net.WebException ex)
  115. {
  116. throw new Exception(apiName + "调用失败," + ex.Message);
  117. }
  118. }
  119. /// <summary>
  120. /// post接口调用方法
  121. /// </summary>
  122. /// <param name="apiName"></param>
  123. /// <param name="url"></param>
  124. /// <param name="body"></param>
  125. /// <param name="webHeader"></param>
  126. /// <returns></returns>
  127. public static string HttpPost(string apiName, string url, string body, WebHeaderCollection webHeader = null)
  128. {
  129. ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
  130. try
  131. {
  132. Encoding encoding = Encoding.UTF8;
  133. System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
  134. request.Method = "POST";
  135. if (url.StartsWith("https://", StringComparison.InvariantCultureIgnoreCase))
  136. {
  137. request.ProtocolVersion = HttpVersion.Version11;
  138. request.ServerCertificateValidationCallback = (a, b, c, d) => true;
  139. }
  140. request.Accept = "application/json, text/javascript, */*";
  141. request.ContentType = "application/json; charset=utf-8";
  142. var timeout = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["TimeOut"]);//超时时间
  143. if (timeout > 0)
  144. request.Timeout = timeout;
  145. if (webHeader != null)
  146. request.Headers = webHeader;
  147. byte[] buffer = encoding.GetBytes(body);
  148. request.ContentLength = buffer.Length;
  149. request.GetRequestStream().Write(buffer, 0, buffer.Length);
  150. System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
  151. using (System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream(), encoding))
  152. {
  153. return reader.ReadToEnd();
  154. }
  155. }
  156. catch (System.Net.WebException ex)
  157. {
  158. throw new Exception(apiName + "调用失败," + ex.Message);
  159. }
  160. }
  161. /// <summary>
  162. /// 获取ID
  163. /// </summary>
  164. /// <returns></returns>
  165. public static string GetNewid()
  166. {
  167. string sql = "select newid() AS ID";
  168. return SqlHelper.ExecuteScalar(sql).ToString();
  169. }
  170. }
  171. }