WebApiHelper.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. using Newtonsoft.Json.Linq;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Runtime.Serialization.Json;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Web;
  11. using WCS.Data;
  12. namespace WCS.WMSWorkflow
  13. {
  14. public enum RequestMethod
  15. {
  16. Get = 1,//获取
  17. Post = 2,//投寄
  18. OPTIONS = 3,//选项
  19. HEAD = 4,//头
  20. PUT = 5,//放置
  21. DELETE = 6,//删除
  22. TRACE = 7,//跟踪
  23. CONNECT = 8,//连接
  24. }
  25. public class WebApiHelper
  26. {
  27. //internal static string MesBlankDownAddress = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["MesBlankDown"]);
  28. //application/x-www-form-urlencoded//application/json
  29. /// <summary>
  30. /// webapi地址调用函数
  31. /// </summary>
  32. /// <param name="url">地址</param>
  33. /// <param name="user_Id">验证用户</param>
  34. /// <param name="reqMethod">请求方式</param>
  35. /// <param name="param">参数(json)</param>
  36. /// <returns></returns>
  37. internal static JObject SendInfoToWebAPI(string url, string user_Id = null, string reqMethod = "Get", JArray param = null)
  38. {
  39. //JArray jsonobj =new JArray();
  40. JObject jobj = null;
  41. HttpWebRequest webRequest = null;
  42. HttpWebResponse webResponse = null;
  43. try
  44. {
  45. webRequest = (HttpWebRequest)HttpWebRequest.Create(url);
  46. //webRequest.ContentType = "application/x-www-form-urlencoded";
  47. webRequest.ContentType = "application/json";
  48. //webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727) ";
  49. webRequest.Method = reqMethod;
  50. webRequest.AllowAutoRedirect = false;
  51. webRequest.Timeout = 50000;
  52. byte[] PostData = null;
  53. if (user_Id != null)
  54. webRequest.Headers.Add("Authorization", user_Id);
  55. //webRequest.Headers.Add("Authorization", "A1203016");
  56. if (param != null)
  57. {
  58. //string paraUrlCoded = System.Web.HttpUtility.UrlEncode("paramaters");
  59. //paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode(jsonParas);
  60. PostData = Encoding.UTF8.GetBytes(param[0].ToString());
  61. webRequest.ContentLength = PostData.Length;
  62. }
  63. if (reqMethod == RequestMethod.Post.ToString())
  64. {
  65. Stream RequestStream = webRequest.GetRequestStream();
  66. if (PostData != null)
  67. {
  68. RequestStream.Write(PostData, 0, PostData.Length);
  69. }
  70. RequestStream.Close();
  71. RequestStream.Dispose();
  72. }
  73. webResponse = (HttpWebResponse)webRequest.GetResponse();
  74. Stream ResponseStrem = webResponse.GetResponseStream();
  75. StreamReader reader = new StreamReader(ResponseStrem);
  76. string ValueString = reader.ReadToEnd();
  77. jobj = JObject.Parse(ValueString);
  78. //jsonobj = JArray.Parse(ValueString);
  79. ResponseStrem.Dispose();
  80. webResponse.Close();
  81. }
  82. catch (ThreadInterruptedException threx)
  83. {
  84. string messge = string.Format("调用接口超时,线程被中断,Url=[{0}]", url);
  85. Log4netHelper.Logger_Error.Error(messge);
  86. }
  87. catch (WebException webex)
  88. {
  89. //webResponse = (HttpWebResponse)webex.Response;
  90. //StreamReader sr = new StreamReader(webResponse.GetResponseStream());
  91. //string temp = sr.ReadToEnd();
  92. string messge = string.Format("通信接口错误[{0}],Url=[{1}]", webex.Message, url);
  93. Log4netHelper.Logger_Error.Error(messge);
  94. }
  95. catch (Exception ex)
  96. {
  97. //string messge = string.Format("通信接口错误,Url={0}", url);
  98. Log4netHelper.Logger_Error.Error(ex.ToString());
  99. }
  100. finally
  101. {
  102. if (webResponse != null)
  103. {
  104. webResponse.Close();
  105. webResponse = null;
  106. }
  107. if (webRequest != null)
  108. {
  109. webRequest.Abort();
  110. webRequest = null;
  111. }
  112. }
  113. return jobj;
  114. }
  115. internal static JObject SendInfoToAPI(string url, string user_Id = null, string reqMethod = "Get", JArray param = null)
  116. {
  117. JObject jobj = null;
  118. HttpWebRequest webRequest = null;
  119. HttpWebResponse webResponse = null;
  120. try
  121. {
  122. webRequest = (HttpWebRequest)WebRequest.Create(url);
  123. //webRequest.ContentType = "application/x-www-form-urlencoded";
  124. webRequest.ContentType = "application/json";
  125. //webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727) ";
  126. webRequest.Method = reqMethod;
  127. webRequest.AllowAutoRedirect = false;
  128. webRequest.Timeout = 500000;
  129. byte[] PostData = null;
  130. if (user_Id != null)
  131. webRequest.Headers.Add("Authorization", user_Id);
  132. //webRequest.Headers.Add("Authorization", "A1203016");
  133. if (param != null)
  134. {
  135. //string paraUrlCoded = System.Web.HttpUtility.UrlEncode("paramaters");
  136. //paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode(jsonParas);
  137. PostData = Encoding.UTF8.GetBytes(param[0].ToString());
  138. webRequest.ContentLength = PostData.Length;
  139. }
  140. if (reqMethod == RequestMethod.Post.ToString())
  141. {
  142. Stream RequestStream = webRequest.GetRequestStream();
  143. if (PostData != null)
  144. {
  145. RequestStream.Write(PostData, 0, PostData.Length);
  146. }
  147. RequestStream.Close();
  148. RequestStream.Dispose();
  149. }
  150. webResponse = (HttpWebResponse)webRequest.GetResponse();
  151. Stream ResponseStrem = webResponse.GetResponseStream();
  152. StreamReader reader = new StreamReader(ResponseStrem);
  153. string ValueString = reader.ReadToEnd();
  154. jobj = JObject.Parse(ValueString);
  155. //jsonobj = JArray.Parse(ValueString);
  156. ResponseStrem.Dispose();
  157. webResponse.Close();
  158. }
  159. catch (ThreadInterruptedException threx)
  160. {
  161. string messge = string.Format("调用接口超时,线程被中断,Url=[{0}]", url);
  162. Log4netHelper.Logger_Error.Error(messge);
  163. throw new Exception(messge);
  164. }
  165. catch (WebException webex)
  166. {
  167. string messge = string.Format("通信接口错误[{0}],Url=[{1}]", webex.Message, url);
  168. Log4netHelper.Logger_Error.Error(messge);
  169. throw new Exception(messge);
  170. }
  171. catch (Exception ex)
  172. {
  173. Log4netHelper.Logger_Error.Error(ex.ToString());
  174. throw new Exception(ex.Message);
  175. }
  176. finally
  177. {
  178. if (webResponse != null)
  179. {
  180. webResponse.Close();
  181. webResponse = null;
  182. }
  183. if (webRequest != null)
  184. {
  185. webRequest.Abort();
  186. webRequest = null;
  187. }
  188. }
  189. return jobj;
  190. }
  191. internal static JObject SendAsyncInfoToWebAPI(string url, string user_Id = null, string reqMethod = "Get", JArray param = null)
  192. {
  193. //JArray jsonobj =new JArray();
  194. JObject jobj = null;
  195. HttpWebRequest webRequest = null;
  196. HttpWebResponse webResponse = null;
  197. try
  198. {
  199. webRequest = (HttpWebRequest)HttpWebRequest.Create(url);
  200. webRequest.ContentType = "application/x-www-form-urlencoded";
  201. webRequest.Method = reqMethod;
  202. webRequest.AllowAutoRedirect = false;
  203. webRequest.Timeout = 5000;
  204. byte[] PostData = null;
  205. if (user_Id != null)
  206. webRequest.Headers.Add("Authorization", user_Id);
  207. //webRequest.Headers.Add("Authorization", "A1203016");
  208. if (param != null)
  209. {
  210. //string paraUrlCoded = System.Web.HttpUtility.UrlEncode("paramaters");
  211. //paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode(jsonParas);
  212. PostData = Encoding.UTF8.GetBytes(param[0].ToString());
  213. webRequest.ContentLength = PostData.Length;
  214. }
  215. if (reqMethod == RequestMethod.Post.ToString())
  216. {
  217. Stream RequestStream = webRequest.GetRequestStream();
  218. if (PostData != null)
  219. {
  220. RequestStream.Write(PostData, 0, PostData.Length);
  221. }
  222. RequestStream.Close();
  223. RequestStream.Dispose();
  224. }
  225. //webResponse = (HttpWebResponse)webRequest.GetResponse();
  226. webRequest.BeginGetResponse(AsyncCallbackResponse, webRequest);
  227. //Stream ResponseStrem = webResponse.GetResponseStream();
  228. //StreamReader reader = new StreamReader(ResponseStrem);
  229. //string ValueString = reader.ReadToEnd();
  230. //jobj = JObject.Parse(ValueString);
  231. //jsonobj = JArray.Parse(ValueString);
  232. //ResponseStrem.Dispose();
  233. webResponse.Close();
  234. }
  235. catch (WebException webex)
  236. {
  237. webResponse = (HttpWebResponse)webex.Response;
  238. StreamReader sr = new StreamReader(webResponse.GetResponseStream());
  239. string temp = sr.ReadToEnd();
  240. string messge = string.Format("通信接口错误,Url={0}", url);
  241. }
  242. catch (Exception ex)
  243. {
  244. string messge = string.Format("通信接口错误,Url={0}", url);
  245. }
  246. finally
  247. {
  248. if (webResponse != null)
  249. {
  250. webResponse.Close();
  251. webResponse = null;
  252. }
  253. if (webRequest != null)
  254. {
  255. webRequest.Abort();
  256. webRequest = null;
  257. }
  258. }
  259. return jobj;
  260. }
  261. internal static JArray SendInfoToWebAPIList(string url, string user_Id = null, string reqMethod = "Get", JArray param = null)
  262. {
  263. JArray jsonobj =new JArray();
  264. //JObject jobj = null;
  265. HttpWebRequest webRequest = null;
  266. HttpWebResponse webResponse = null;
  267. try
  268. {
  269. webRequest = (HttpWebRequest)HttpWebRequest.Create(url);
  270. //webRequest.ContentType = "application/x-www-form-urlencoded";
  271. webRequest.ContentType = "application/json";
  272. //webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727) ";
  273. webRequest.Method = reqMethod;
  274. webRequest.AllowAutoRedirect = false;
  275. webRequest.Timeout = 50000;
  276. byte[] PostData = null;
  277. if (user_Id != null)
  278. webRequest.Headers.Add("Authorization", user_Id);
  279. //webRequest.Headers.Add("Authorization", "A1203016");
  280. if (param != null)
  281. {
  282. //string paraUrlCoded = System.Web.HttpUtility.UrlEncode("paramaters");
  283. //paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode(jsonParas);
  284. PostData = Encoding.UTF8.GetBytes(param[0].ToString());
  285. webRequest.ContentLength = PostData.Length;
  286. }
  287. if (reqMethod == RequestMethod.Post.ToString())
  288. {
  289. Stream RequestStream = webRequest.GetRequestStream();
  290. if (PostData != null)
  291. {
  292. RequestStream.Write(PostData, 0, PostData.Length);
  293. }
  294. RequestStream.Close();
  295. RequestStream.Dispose();
  296. }
  297. webResponse = (HttpWebResponse)webRequest.GetResponse();
  298. Stream ResponseStrem = webResponse.GetResponseStream();
  299. StreamReader reader = new StreamReader(ResponseStrem);
  300. string ValueString = reader.ReadToEnd();
  301. //jobj = JObject.Parse(ValueString);
  302. jsonobj = JArray.Parse(ValueString);
  303. ResponseStrem.Dispose();
  304. webResponse.Close();
  305. }
  306. catch (WebException webex)
  307. {
  308. //webResponse = (HttpWebResponse)webex.Response;
  309. //StreamReader sr = new StreamReader(webResponse.GetResponseStream());
  310. //string temp = sr.ReadToEnd();
  311. string messge = string.Format("通信接口错误,Url={0}", url);
  312. }
  313. catch (Exception ex)
  314. {
  315. string messge = string.Format("通信接口错误,Url={0}", url);
  316. }
  317. finally
  318. {
  319. if (webResponse != null)
  320. {
  321. webResponse.Close();
  322. webResponse = null;
  323. }
  324. if (webRequest != null)
  325. {
  326. webRequest.Abort();
  327. webRequest = null;
  328. }
  329. }
  330. return jsonobj;
  331. }
  332. private static void AsyncCallbackResponse(IAsyncResult ar)
  333. {
  334. HttpWebRequest webrequest = ar.AsyncState as HttpWebRequest;
  335. var webresponse = webrequest.EndGetResponse(ar) as HttpWebResponse;
  336. Stream ResponseStrem = webresponse.GetResponseStream();
  337. using (StreamReader reader = new StreamReader(ResponseStrem))
  338. {
  339. string ValueString = reader.ReadToEnd();
  340. }
  341. }
  342. /// <summary>
  343. /// 生成Json格式
  344. /// </summary>
  345. public static string GetJson(object obj)
  346. {
  347. DataContractJsonSerializer json = new DataContractJsonSerializer(obj.GetType());
  348. using (MemoryStream stream = new MemoryStream())
  349. {
  350. json.WriteObject(stream, obj);
  351. string szJson = Encoding.UTF8.GetString(stream.ToArray());
  352. return szJson;
  353. }
  354. }
  355. /// <summary>
  356. /// 生成Json格式
  357. /// </summary>
  358. public static string GetJson_2<T>(T t)
  359. {
  360. DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(T));
  361. using (MemoryStream stream = new MemoryStream())
  362. {
  363. json.WriteObject(stream, t);
  364. string szJson = Encoding.UTF8.GetString(stream.ToArray());
  365. return szJson;
  366. }
  367. }
  368. /// <summary>
  369. /// Json转Model
  370. /// </summary>
  371. public static T ParseFromJson<T>(string szJson)
  372. {
  373. T obj = Activator.CreateInstance<T>();
  374. using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(szJson)))
  375. {
  376. DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
  377. return (T)serializer.ReadObject(ms);
  378. }
  379. }
  380. /// <summary>
  381. /// Json转List
  382. /// </summary>
  383. public static List<T> ParseToListFromJson<T>(JArray ja)
  384. {
  385. var result = new List<T>();
  386. foreach (var item in ja)
  387. {
  388. result.Add(ParseFromJson<T>(item.ToString()));
  389. }
  390. return result;
  391. }
  392. }
  393. }