diff --git a/.vs/ICSSoft.FromERP/v16/.suo b/.vs/ICSSoft.FromERP/v16/.suo index 95e0e7b..d9031a3 100644 Binary files a/.vs/ICSSoft.FromERP/v16/.suo and b/.vs/ICSSoft.FromERP/v16/.suo differ diff --git a/ICSSoft.FromERP/App.config b/ICSSoft.FromERP/App.config index 4c4ab2f..5ed0c42 100644 --- a/ICSSoft.FromERP/App.config +++ b/ICSSoft.FromERP/App.config @@ -12,6 +12,9 @@ + + + diff --git a/ICSSoft.FromERP/HttpHelper.cs b/ICSSoft.FromERP/HttpHelper.cs index 3ed4bfa..7284e46 100644 --- a/ICSSoft.FromERP/HttpHelper.cs +++ b/ICSSoft.FromERP/HttpHelper.cs @@ -8,12 +8,13 @@ using System.Net; using System.Reflection; using System.Text; using System.Threading.Tasks; +using System.Net.Http.Headers; namespace ICSSoft.FromERP { public class HttpHelper { - + /// /// POST请求 /// @@ -21,7 +22,7 @@ namespace ICSSoft.FromERP /// 请求json /// token /// - public static async Task HttpClientPost(string url, string requestJson, string contentType = "application/json", string token = "") where T : new() + public static async Task HttpClientPost(string url, string requestJson, Dictionary dic=null , string contentType = "application/json", string token = "") where T : new() { string result = string.Empty; @@ -29,20 +30,87 @@ namespace ICSSoft.FromERP using (HttpContent httpContent = new StringContent(requestJson, System.Text.Encoding.UTF8, contentType)) { //使用注入的httpclientfactory获取client - using (var httpClient = new HttpClient()) + using (var httpClient = new HttpClient( )) { + + if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) + { + ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true; + ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; + // httpClient.BaseAddress. = HttpVersion.Version10; + // httpClient.DefaultRequestVersion = HttpVersion.Version30, + } + //设置请求头 //设置超时时间 if (!string.IsNullOrEmpty(token)) httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + token); + if (dic != null) + { + foreach (var item in dic) + { + httpClient.DefaultRequestHeaders.Add(item.Key, item.Value); + } + } + httpClient.Timeout = new TimeSpan(0, 0, 60); HttpResponseMessage res = httpClient.PostAsync(url, httpContent).Result; res.EnsureSuccessStatusCode(); - result = res.Content.ReadAsStringAsync().Result; + result = res.Content.ReadAsStringAsync().Result; + return JsonConvert.DeserializeObject(result); + } + } + + } + + public static async Task HttpClientPost2(string url, string requestJson, Dictionary dic, string contentType = "application/json") where T : new() + { + + string result = string.Empty; + Uri postUrl = new Uri(url); + + //使用注入的httpclientfactory获取client + using (var httpClient = new HttpClient()) + { + httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType)); + + using (StringContent strcontent = new StringContent(requestJson, Encoding.UTF8, contentType)) + { + var message = new HttpRequestMessage(HttpMethod.Post, url); + //设置cookie信息 + foreach (var item in dic) + { + message.Headers.Add(item.Key, item.Value); + } + + //设置contetn + message.Content = strcontent; + //发送请求 + var res = httpClient.SendAsync(message).Result; + res.EnsureSuccessStatusCode(); + result = await res.Content.ReadAsStringAsync(); return JsonConvert.DeserializeObject(result); } + + ////设置请求头 + ////设置超时时间 + //if (dic != null && dic.Count > 0) + //{ + // foreach (var item in dic) + // { + // httpClient.DefaultRequestHeaders.Add(item.Key, item.Value); + // } + //} + ////if (!string.IsNullOrEmpty(token)) + //// httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + token); + //httpClient.Timeout = new TimeSpan(0, 0, 60); + //HttpResponseMessage res = httpClient.PostAsync(url, httpContent).Result; + //res.EnsureSuccessStatusCode(); + //result = await res.Content.ReadAsStringAsync(); + //return JsonConvert.DeserializeObject(result); } + } public static async Task HttpClientGet(string url, string contentType = "application/json", string token = "") where T : new() @@ -54,19 +122,20 @@ namespace ICSSoft.FromERP httpClient.Timeout = new TimeSpan(0, 0, 60); HttpResponseMessage res = httpClient.GetAsync(url).Result; res.EnsureSuccessStatusCode(); - var t = res.Content.ReadAsStringAsync().Result; + var t = res.Content.ReadAsStringAsync().Result; return JsonConvert.DeserializeObject(t); }; } - public static async Task PostForm(string Url, R message) where T : new() + public static async Task PostForm(string Url, R message, Dictionary dic=null) where T : new() { var res = new T(); using (HttpClient httpClient = new HttpClient()) { + httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded")); var body = new List>(); foreach (PropertyInfo info in typeof(R).GetProperties()) { @@ -75,21 +144,98 @@ namespace ICSSoft.FromERP } var content = new FormUrlEncodedContent(body); httpClient.DefaultRequestHeaders.Add("Method", "Post"); - HttpResponseMessage response = await httpClient.PostAsync(Url, content); + if (dic != null) + { + foreach (var item in dic) + { + httpClient.DefaultRequestHeaders.Add(item.Key, item.Value); + } + } + HttpResponseMessage response = httpClient.PostAsync(Url, content).Result; if ((int)response.StatusCode == 200) { response.EnsureSuccessStatusCode(); - string result = response.Content.ReadAsStringAsync().Result; - res = JsonConvert.DeserializeObject(result); + } - else + string result = response.Content.ReadAsStringAsync().Result; + res = JsonConvert.DeserializeObject(result); + + } + return res; + } + + public static async Task HttpPostNoFile(string url, string data) + { + // Encoding encoding = Encoding.UTF8; + // string jsonParam = data; + HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; + request.Method = "post"; + request.ContentType = "application/json"; + + byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString()); + request.ContentLength = byteData.Length; + + using (Stream postStream = request.GetRequestStream()) + { + postStream.Write(byteData, 0, byteData.Length); + } + + using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) + { + using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { - string result = response.Content.ReadAsStringAsync().Result; - res = JsonConvert.DeserializeObject(result); + var result= reader.ReadToEnd().ToString(); + + return JsonConvert.DeserializeObject(result); } } - return res; + } + + public static string PostData(byte[] data, string url, string contentType = "application/json", int timeout = 20) + { + //创建httpWebRequest对象 + HttpWebRequest httpRequest = null; + if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) + { + ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true; + ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; + httpRequest = WebRequest.Create(url) as HttpWebRequest; + httpRequest.ProtocolVersion = HttpVersion.Version10; + } + else + { + httpRequest = WebRequest.Create(url) as HttpWebRequest; + } + if (httpRequest == null) + { + throw new ApplicationException(string.Format("Invalid url string: {0}", url)); + } + //填充httpWebRequest的基本信息 + httpRequest.ContentType = contentType; + httpRequest.Method = "POST"; + httpRequest.Timeout = timeout * 1000; + //填充并发送要post的内容 + httpRequest.ContentLength = data.Length; + httpRequest.Headers.Add("deipaaskeyauth", "a5P1RTL4380zd9jpb57qXx63rdynUHN2"); + using (Stream requestStream = httpRequest.GetRequestStream()) + { + requestStream.Write(data, 0, data.Length); + requestStream.Close(); + } + //发送post请求到服务器并读取服务器返回信息 + var response = httpRequest.GetResponse(); + using (Stream responseStream = response.GetResponseStream()) + { + //读取服务器返回信息 + string stringResponse = string.Empty; + using (StreamReader responseReader = new StreamReader(responseStream, Encoding.UTF8)) + { + stringResponse = responseReader.ReadToEnd(); + } + responseStream.Close(); + return stringResponse; + } } } } diff --git a/ICSSoft.FromERP/ICSSoft.FromERP.csproj b/ICSSoft.FromERP/ICSSoft.FromERP.csproj index 4bd2b6d..abd927a 100644 --- a/ICSSoft.FromERP/ICSSoft.FromERP.csproj +++ b/ICSSoft.FromERP/ICSSoft.FromERP.csproj @@ -26,7 +26,7 @@ true full false - ..\Root\ + bin\Debug\ DEBUG;TRACE prompt 4 diff --git a/ICSSoft.FromERP/SyncCas_Jinyang.cs b/ICSSoft.FromERP/SyncCas_Jinyang.cs index 81648f6..298dbcd 100644 --- a/ICSSoft.FromERP/SyncCas_Jinyang.cs +++ b/ICSSoft.FromERP/SyncCas_Jinyang.cs @@ -44,50 +44,51 @@ namespace ICSSoft.FromERP DataTable dt = ICSHelper.GetERPDB(conStr); foreach (DataRow dr in dt.Rows) { - var dtNowBegin = new DateTime(2000, 1, 1, 0, 0, 0);//默认开始时间 - var dtNow = DateTime.Now; - string erpName = ICSHelper.GetConfigString()["ERPDB"]; - string TenantId = dr["TenantId"].ToString();//mes 组织 - string TenantCode = dr["TenantCode"].ToString(); - string ErpId = dr["ErpID"].ToString(); //erpID - string Class = this.GetType().Name + TenantCode; - erpName = string.Format(erpName, TenantId); + //var dtNowBegin = new DateTime(2000, 1, 1, 0, 0, 0);//默认开始时间 + //var dtNow = DateTime.Now; + //string erpName = ICSHelper.GetConfigString()["ERPDB"]; + //string TenantId = dr["TenantId"].ToString();//mes 组织 + //string TenantCode = dr["TenantCode"].ToString(); + //string ErpId = dr["ErpID"].ToString(); //erpID + //string Class = this.GetType().Name + TenantCode; + //erpName = string.Format(erpName, TenantId); - string sql0 = " SELECT top 1 ModifyDate FROM ICSERPTime where ClassName='" + Class + "'"; - var lastDate = ICSHelper.ExecuteScalar(conStr, sql0).ToDateOrNull(); - if (!lastDate.HasValue) - { - lastDate = dtNowBegin; - } + //string sql0 = " SELECT top 1 ModifyDate FROM ICSERPTime where ClassName='" + Class + "'"; + //var lastDate = ICSHelper.ExecuteScalar(conStr, sql0).ToDateOrNull(); + //if (!lastDate.HasValue) + //{ + // lastDate = dtNowBegin; + //} - string sql = @" select '" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' as MTIME into #TempVendor "; - sql = ICSHelper.Time(Namespace, Class, TenantId, sql, "#TempVendor"); - sql += "DROP TABLE #TempVendor"; - ICSHelper.ExecuteDate(conStr, sql); + //string sql = @" select '" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' as MTIME into #TempVendor "; + //sql = ICSHelper.Time(Namespace, Class, TenantId, sql, "#TempVendor"); + //sql += "DROP TABLE #TempVendor"; + //ICSHelper.ExecuteDate(conStr, sql); + List list = new List(); var input = new CapInputDto(); - input.WorkshopID = ""; - input.EquipmentID = ""; - input.ProductNo = ""; - input.LotNo = ""; - input.CollectionTime = ""; + input.WorkshopID = "1"; + input.EquipmentID = "1"; + input.ProductNo = "1"; + input.LotNo = "1"; + input.CollectionTime = "1"; input.ParamValue_Resistance = 12; input.ParamValue_Height = 12; input.Msg = ""; - var inputObj = JsonConvert.SerializeObject(input); - string url = ICSHelper.GetConfigString()["WeiMasErpUrl"] + @"/BasicAssistingService/FromWeMec/APIVenderToMES"; + list.Add(input); + + var inputObj = JsonConvert.SerializeObject(list); + //string url = ICSHelper.GetConfigString()["JinyangCapUrl"]+ @"?deipaaskeyauth=a5P1RTL4380zd9jpb57qXx63rdynUHN2"; + string url = ICSHelper.GetConfigString()["JinyangCapUrl"]; - var result = HttpHelper.HttpClientPost(url, inputObj).Result; + var result = HttpHelper.HttpClientPost(url, inputObj, new Dictionary() { { "deipaaskeyauth", "a5P1RTL4380zd9jpb57qXx63rdynUHN2" } }).Result; + // var result = HttpHelper.HttpClientPost(url, inputObj).Result; if (result.Result == "OK") { - - - // insertSql += "DROP TABLE #tempFromErp4Vendor"; - //ICSHelper.ExecuteDate(conStr, insertSql); } else { diff --git a/ICSSoft.Test/Program.cs b/ICSSoft.Test/Program.cs index d593f5c..0dba83b 100644 --- a/ICSSoft.Test/Program.cs +++ b/ICSSoft.Test/Program.cs @@ -13,7 +13,7 @@ namespace ICSSoft.Test //ICSVendor test = new ICSVendor(); //test.Execute(); - SyncCustomer_WeiMas test = new SyncCustomer_WeiMas(); + SyncCas_Jinyang test = new SyncCas_Jinyang(); test.Execute(); // var ss= ICSHelper.ApiSign("sj_w_id", "70FF7F01C5899A5ACAAD4ECB7FA5B3C5", 1724650261);