| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 | 
							- using Newtonsoft.Json.Linq;
 
- using System;
 
- using System.Collections.Generic;
 
- using System.IO;
 
- using System.Linq;
 
- using System.Net;
 
- using System.Runtime.Serialization.Json;
 
- using System.Text;
 
- using System.Threading;
 
- using System.Web;
 
- using WCS.Data;
 
- namespace WCS.WMSWorkflow
 
- {
 
-     public enum RequestMethod
 
-     {
 
-         Get = 1,//获取
 
-         Post = 2,//投寄
 
-         OPTIONS = 3,//选项
 
-         HEAD = 4,//头
 
-         PUT = 5,//放置
 
-         DELETE = 6,//删除
 
-         TRACE = 7,//跟踪
 
-         CONNECT = 8,//连接
 
-     }
 
-     public class WebApiHelper
 
-     {
 
-         //internal static string MesBlankDownAddress = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["MesBlankDown"]);
 
-         //application/x-www-form-urlencoded//application/json
 
-         /// <summary>
 
-         /// webapi地址调用函数
 
-         /// </summary>
 
-         /// <param name="url">地址</param>
 
-         /// <param name="user_Id">验证用户</param>
 
-         /// <param name="reqMethod">请求方式</param>
 
-         /// <param name="param">参数(json)</param>
 
-         /// <returns></returns>
 
-         internal static JObject SendInfoToWebAPI(string url, string user_Id = null, string reqMethod = "Get", JArray param = null)
 
-         {
 
-             //JArray jsonobj =new JArray();
 
-             JObject jobj = null;
 
-             HttpWebRequest webRequest = null;
 
-             HttpWebResponse webResponse = null;
 
-             try
 
-             {
 
-                 webRequest = (HttpWebRequest)HttpWebRequest.Create(url);
 
-                 //webRequest.ContentType = "application/x-www-form-urlencoded";
 
-                 webRequest.ContentType = "application/json";
 
-                 //webRequest.UserAgent = "Mozilla/4.0   (compatible;   MSIE   6.0;   Windows   NT   5.1;   SV1;   .NET   CLR  2.0.50727) ";
 
-                 webRequest.Method = reqMethod;
 
-                 webRequest.AllowAutoRedirect = false;
 
-                 webRequest.Timeout = 50000;
 
-                 byte[] PostData = null;
 
-                 if (user_Id != null)
 
-                     webRequest.Headers.Add("Authorization", user_Id);
 
-                 //webRequest.Headers.Add("Authorization", "A1203016");
 
-                 if (param != null)
 
-                 {
 
-                     //string paraUrlCoded = System.Web.HttpUtility.UrlEncode("paramaters");
 
-                     //paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode(jsonParas);
 
-                     PostData = Encoding.UTF8.GetBytes(param[0].ToString());
 
-                     webRequest.ContentLength = PostData.Length;
 
-                 }
 
-                 if (reqMethod == RequestMethod.Post.ToString())
 
-                 {
 
-                     Stream RequestStream = webRequest.GetRequestStream();
 
-                     if (PostData != null)
 
-                     {
 
-                         RequestStream.Write(PostData, 0, PostData.Length);
 
-                     }
 
-                     RequestStream.Close();
 
-                     RequestStream.Dispose();
 
-                 }
 
-                 webResponse = (HttpWebResponse)webRequest.GetResponse();
 
-                 Stream ResponseStrem = webResponse.GetResponseStream();
 
-                 StreamReader reader = new StreamReader(ResponseStrem);
 
-                 string ValueString = reader.ReadToEnd();
 
-                 jobj = JObject.Parse(ValueString);
 
-                 //jsonobj = JArray.Parse(ValueString);
 
-                 ResponseStrem.Dispose();
 
-                 webResponse.Close();
 
-             }
 
-             catch (ThreadInterruptedException threx)
 
-             {
 
-                 string messge = string.Format("调用接口超时,线程被中断,Url=[{0}]", url);
 
-                 Log4netHelper.Logger_Error.Error(messge);
 
-             }
 
-             catch (WebException webex)
 
-             {
 
-                 //webResponse = (HttpWebResponse)webex.Response;
 
-                 //StreamReader sr = new StreamReader(webResponse.GetResponseStream());
 
-                 //string temp = sr.ReadToEnd();
 
-                 string messge = string.Format("通信接口错误[{0}],Url=[{1}]", webex.Message, url);
 
-                 Log4netHelper.Logger_Error.Error(messge);
 
-             }
 
-             catch (Exception ex)
 
-             {
 
-                 //string messge = string.Format("通信接口错误,Url={0}", url);
 
-                 Log4netHelper.Logger_Error.Error(ex.ToString());
 
-             }
 
-             finally
 
-             {
 
-                 if (webResponse != null)
 
-                 {
 
-                     webResponse.Close();
 
-                     webResponse = null;
 
-                 }
 
-                 if (webRequest != null)
 
-                 {
 
-                     webRequest.Abort();
 
-                     webRequest = null;
 
-                 }
 
-             }
 
-             return jobj;
 
-         }
 
-         internal static JObject SendInfoToAPI(string url, string user_Id = null, string reqMethod = "Get", JArray param = null)
 
-         {
 
-             JObject jobj = null;
 
-             HttpWebRequest webRequest = null;
 
-             HttpWebResponse webResponse = null;
 
-             try
 
-             {
 
-                 webRequest = (HttpWebRequest)WebRequest.Create(url);
 
-                 //webRequest.ContentType = "application/x-www-form-urlencoded";
 
-                 webRequest.ContentType = "application/json";
 
-                 //webRequest.UserAgent = "Mozilla/4.0   (compatible;   MSIE   6.0;   Windows   NT   5.1;   SV1;   .NET   CLR  2.0.50727) ";
 
-                 webRequest.Method = reqMethod;
 
-                 webRequest.AllowAutoRedirect = false;
 
-                 webRequest.Timeout = 500000;
 
-                 byte[] PostData = null;
 
-                 if (user_Id != null)
 
-                     webRequest.Headers.Add("Authorization", user_Id);
 
-                 //webRequest.Headers.Add("Authorization", "A1203016");
 
-                 if (param != null)
 
-                 {
 
-                     //string paraUrlCoded = System.Web.HttpUtility.UrlEncode("paramaters");
 
-                     //paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode(jsonParas);
 
-                     PostData = Encoding.UTF8.GetBytes(param[0].ToString());
 
-                     webRequest.ContentLength = PostData.Length;
 
-                 }
 
-                 if (reqMethod == RequestMethod.Post.ToString())
 
-                 {
 
-                     Stream RequestStream = webRequest.GetRequestStream();
 
-                     if (PostData != null)
 
-                     {
 
-                         RequestStream.Write(PostData, 0, PostData.Length);
 
-                     }
 
-                     RequestStream.Close();
 
-                     RequestStream.Dispose();
 
-                 }
 
-                 webResponse = (HttpWebResponse)webRequest.GetResponse();
 
-                 Stream ResponseStrem = webResponse.GetResponseStream();
 
-                 StreamReader reader = new StreamReader(ResponseStrem);
 
-                 string ValueString = reader.ReadToEnd();
 
-                 jobj = JObject.Parse(ValueString);
 
-                 //jsonobj = JArray.Parse(ValueString);
 
-                 ResponseStrem.Dispose();
 
-                 webResponse.Close();
 
-             }
 
-             catch (ThreadInterruptedException threx)
 
-             {
 
-                 string messge = string.Format("调用接口超时,线程被中断,Url=[{0}]", url);
 
-                 Log4netHelper.Logger_Error.Error(messge);
 
-                 throw new Exception(messge);
 
-             }
 
-             catch (WebException webex)
 
-             {
 
-                 string messge = string.Format("通信接口错误[{0}],Url=[{1}]", webex.Message, url);
 
-                 Log4netHelper.Logger_Error.Error(messge);
 
-                 throw new Exception(messge);
 
-             }
 
-             catch (Exception ex)
 
-             {
 
-                 Log4netHelper.Logger_Error.Error(ex.ToString());
 
-                 throw new Exception(ex.Message);
 
-             }
 
-             finally
 
-             {
 
-                 if (webResponse != null)
 
-                 {
 
-                     webResponse.Close();
 
-                     webResponse = null;
 
-                 }
 
-                 if (webRequest != null)
 
-                 {
 
-                     webRequest.Abort();
 
-                     webRequest = null;
 
-                 }
 
-             }
 
-             return jobj;
 
-         }
 
-         internal static JObject SendAsyncInfoToWebAPI(string url, string user_Id = null, string reqMethod = "Get", JArray param = null)
 
-         {
 
-             //JArray jsonobj =new JArray();
 
-             JObject jobj = null;
 
-             HttpWebRequest webRequest = null;
 
-             HttpWebResponse webResponse = null;
 
-             try
 
-             {
 
-                 webRequest = (HttpWebRequest)HttpWebRequest.Create(url);
 
-                 webRequest.ContentType = "application/x-www-form-urlencoded";
 
-                 webRequest.Method = reqMethod;
 
-                 webRequest.AllowAutoRedirect = false;
 
-                 webRequest.Timeout = 5000;
 
-                 byte[] PostData = null;
 
-                 if (user_Id != null)
 
-                     webRequest.Headers.Add("Authorization", user_Id);
 
-                 //webRequest.Headers.Add("Authorization", "A1203016");
 
-                 if (param != null)
 
-                 {
 
-                     //string paraUrlCoded = System.Web.HttpUtility.UrlEncode("paramaters");
 
-                     //paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode(jsonParas);
 
-                     PostData = Encoding.UTF8.GetBytes(param[0].ToString());
 
-                     webRequest.ContentLength = PostData.Length;
 
-                 }
 
-                 if (reqMethod == RequestMethod.Post.ToString())
 
-                 {
 
-                     Stream RequestStream = webRequest.GetRequestStream();
 
-                     if (PostData != null)
 
-                     {
 
-                         RequestStream.Write(PostData, 0, PostData.Length);
 
-                     }
 
-                     RequestStream.Close();
 
-                     RequestStream.Dispose();
 
-                 }
 
-                 //webResponse = (HttpWebResponse)webRequest.GetResponse();
 
-                 webRequest.BeginGetResponse(AsyncCallbackResponse, webRequest);
 
-                 //Stream ResponseStrem = webResponse.GetResponseStream();
 
-                 //StreamReader reader = new StreamReader(ResponseStrem);
 
-                 //string ValueString = reader.ReadToEnd();
 
-                 //jobj = JObject.Parse(ValueString);
 
-                 //jsonobj = JArray.Parse(ValueString);
 
-                 //ResponseStrem.Dispose();
 
-                 webResponse.Close();
 
-             }
 
-             catch (WebException webex)
 
-             {
 
-                 webResponse = (HttpWebResponse)webex.Response;
 
-                 StreamReader sr = new StreamReader(webResponse.GetResponseStream());
 
-                 string temp = sr.ReadToEnd();
 
-                 string messge = string.Format("通信接口错误,Url={0}", url);
 
-             }
 
-             catch (Exception ex)
 
-             {
 
-                 string messge = string.Format("通信接口错误,Url={0}", url);
 
-             }
 
-             finally
 
-             {
 
-                 if (webResponse != null)
 
-                 {
 
-                     webResponse.Close();
 
-                     webResponse = null;
 
-                 }
 
-                 if (webRequest != null)
 
-                 {
 
-                     webRequest.Abort();
 
-                     webRequest = null;
 
-                 }
 
-             }
 
-             return jobj;
 
-         }
 
-         internal static JArray SendInfoToWebAPIList(string url, string user_Id = null, string reqMethod = "Get", JArray param = null)
 
-         {
 
-             JArray jsonobj =new JArray();
 
-             //JObject jobj = null;
 
-             HttpWebRequest webRequest = null;
 
-             HttpWebResponse webResponse = null;
 
-             try
 
-             {
 
-                 webRequest = (HttpWebRequest)HttpWebRequest.Create(url);
 
-                 //webRequest.ContentType = "application/x-www-form-urlencoded";
 
-                 webRequest.ContentType = "application/json";
 
-                 //webRequest.UserAgent = "Mozilla/4.0   (compatible;   MSIE   6.0;   Windows   NT   5.1;   SV1;   .NET   CLR  2.0.50727) ";
 
-                 webRequest.Method = reqMethod;
 
-                 webRequest.AllowAutoRedirect = false;
 
-                 webRequest.Timeout = 50000;
 
-                 byte[] PostData = null;
 
-                 if (user_Id != null)
 
-                     webRequest.Headers.Add("Authorization", user_Id);
 
-                 //webRequest.Headers.Add("Authorization", "A1203016");
 
-                 if (param != null)
 
-                 {
 
-                     //string paraUrlCoded = System.Web.HttpUtility.UrlEncode("paramaters");
 
-                     //paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode(jsonParas);
 
-                     PostData = Encoding.UTF8.GetBytes(param[0].ToString());
 
-                     webRequest.ContentLength = PostData.Length;
 
-                 }
 
-                 if (reqMethod == RequestMethod.Post.ToString())
 
-                 {
 
-                     Stream RequestStream = webRequest.GetRequestStream();
 
-                     if (PostData != null)
 
-                     {
 
-                         RequestStream.Write(PostData, 0, PostData.Length);
 
-                     }
 
-                     RequestStream.Close();
 
-                     RequestStream.Dispose();
 
-                 }
 
-                 webResponse = (HttpWebResponse)webRequest.GetResponse();
 
-                 Stream ResponseStrem = webResponse.GetResponseStream();
 
-                 StreamReader reader = new StreamReader(ResponseStrem);
 
-                 string ValueString = reader.ReadToEnd();
 
-                 //jobj = JObject.Parse(ValueString);
 
-                 jsonobj = JArray.Parse(ValueString);
 
-                 ResponseStrem.Dispose();
 
-                 webResponse.Close();
 
-             }
 
-             catch (WebException webex)
 
-             {
 
-                 //webResponse = (HttpWebResponse)webex.Response;
 
-                 //StreamReader sr = new StreamReader(webResponse.GetResponseStream());
 
-                 //string temp = sr.ReadToEnd();
 
-                 string messge = string.Format("通信接口错误,Url={0}", url);
 
-             }
 
-             catch (Exception ex)
 
-             {
 
-                 string messge = string.Format("通信接口错误,Url={0}", url);
 
-             }
 
-             finally
 
-             {
 
-                 if (webResponse != null)
 
-                 {
 
-                     webResponse.Close();
 
-                     webResponse = null;
 
-                 }
 
-                 if (webRequest != null)
 
-                 {
 
-                     webRequest.Abort();
 
-                     webRequest = null;
 
-                 }
 
-             }
 
-             return jsonobj;
 
-         }
 
-         private static void AsyncCallbackResponse(IAsyncResult ar)
 
-         {
 
-             HttpWebRequest webrequest = ar.AsyncState as HttpWebRequest;
 
-             var webresponse = webrequest.EndGetResponse(ar) as HttpWebResponse;
 
-             Stream ResponseStrem = webresponse.GetResponseStream();
 
-             using (StreamReader reader = new StreamReader(ResponseStrem))
 
-             {
 
-                 string ValueString = reader.ReadToEnd();
 
-             }
 
-         }
 
-         /// <summary>  
 
-         /// 生成Json格式  
 
-         /// </summary>  
 
-         public static string GetJson(object obj)
 
-         {
 
-             DataContractJsonSerializer json = new DataContractJsonSerializer(obj.GetType());
 
-             using (MemoryStream stream = new MemoryStream())
 
-             {
 
-                 json.WriteObject(stream, obj);
 
-                 string szJson = Encoding.UTF8.GetString(stream.ToArray());
 
-                 return szJson;
 
-             }
 
-         }
 
-         /// <summary>  
 
-         /// 生成Json格式  
 
-         /// </summary>  
 
-         public static string GetJson_2<T>(T t)
 
-         {
 
-             DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(T));
 
-             using (MemoryStream stream = new MemoryStream())
 
-             {
 
-                 json.WriteObject(stream, t);
 
-                 string szJson = Encoding.UTF8.GetString(stream.ToArray());
 
-                 return szJson;
 
-             }
 
-         }
 
-         /// <summary>  
 
-         /// Json转Model  
 
-         /// </summary>  
 
-         public static T ParseFromJson<T>(string szJson)
 
-         {
 
-             T obj = Activator.CreateInstance<T>();
 
-             using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(szJson)))
 
-             {
 
-                 DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
 
-                 return (T)serializer.ReadObject(ms);
 
-             }
 
-         }
 
-         /// <summary>  
 
-         /// Json转List
 
-         /// </summary>  
 
-         public static List<T> ParseToListFromJson<T>(JArray ja)
 
-         {
 
-             var result = new List<T>();
 
-             foreach (var item in ja)
 
-             {
 
-                 result.Add(ParseFromJson<T>(item.ToString()));
 
-             }
 
-             return result;
 
-         }
 
-     }
 
- }
 
 
  |