From 597f8e08e6264b2143454e40a7be553d1e8b6df7 Mon Sep 17 00:00:00 2001
From: sjz <1240968267@qq.com>
Date: 星期五, 09 五月 2025 17:04:19 +0800
Subject: [PATCH] 2025/5/9 沈

---
 StandardInterface/MES.Service/service/PLM/WarehouseDownloadDoc.cs |  145 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 145 insertions(+), 0 deletions(-)

diff --git a/StandardInterface/MES.Service/service/PLM/WarehouseDownloadDoc.cs b/StandardInterface/MES.Service/service/PLM/WarehouseDownloadDoc.cs
new file mode 100644
index 0000000..5527dae
--- /dev/null
+++ b/StandardInterface/MES.Service/service/PLM/WarehouseDownloadDoc.cs
@@ -0,0 +1,145 @@
+锘縰sing Newtonsoft.Json.Linq;
+using System.Net;
+using System.Text;
+
+namespace ConsoleApp1
+{
+    #region 1.鏍规嵁鏂囨。鐗╃悊鏂囦欢id鑾峰彇鏂囨。娴佹暟鎹�
+    public class WarehouseDownloadDoc()
+    {
+        private string _cloudUrl = "http://47.96.178.105/k3cloud";
+        private string _warehouse = "http://183.129.128.254:8081/CloudPLMWarehouse";
+
+        #region 鏂囨。鏈嶅姟鍣ㄤ笂涓嬫枃
+        private string WarehouseCTX
+        {
+            get
+            {
+                string json = "{\"ap1\":{ \"AcctID\":\"" + "68044981e73323" + "\",\"Username\":\"骞挎繁\",\"Password\":\"gs@123456\",\"Lcid\":2052,\"AuthenticateType\":1,\"PasswordIsEncrypted\":\"false\",\"ClientInfo\":{\"ClientType\":8}}";
+                var resp = GetResponse("http://47.96.178.105/k3cloud/Kingdee.BOS.ServiceFacade.ServicesStub.User.UserService.ValidateLoginInfo.common.kdsvc", json, new Dictionary<string, string>());
+                string ret = (new StreamReader(resp.GetResponseStream(), Encoding.UTF8)).ReadToEnd();
+                var result = JObject.Parse(ret)["Context"];
+                string token = result["UserToken"].ToString();
+                var warehouseCtx = string.Format("LoginUrl={0}&KDServiceSessionId={1}", _cloudUrl, JObject.Parse(ret)["KDSVCSessionId"].ToString());//瀹夊叏鏂版柟寮�
+                return Convert.ToBase64String(Encoding.UTF8.GetBytes(warehouseCtx));
+            }
+        }
+        #endregion
+
+        #region 鐗╃悊鏂囦欢id
+        private string GetFileId(string fileId)
+        {
+            return Convert.ToBase64String(Encoding.UTF8.GetBytes(fileId));
+        }
+        #endregion
+
+        #region 鐢靛瓙浠撹闂嚟璇�
+        private string Token
+        {
+            get
+            {
+                var token = Guid.NewGuid().ToString();
+                return token;
+            }
+        }
+        #endregion
+
+        #region 鍙戦�佽姹�
+        public Stream SendRequest(string op, string fileId)
+        {
+            var header = new Dictionary<string, string>()
+            {
+                {"FileID" ,GetFileId(fileId) },
+                { "CTX",WarehouseCTX},
+                { "token", Token },
+                { "PLM_ACCESS_TYPE","pure" }
+            };
+            return PostDataViaHttpWebRequest(RequestUrl(op), string.Empty, header, !op.Equals("Download"));
+        }
+        #endregion
+
+        #region 璇锋眰鐨刄RL
+        private string RequestUrl(string op)
+        {
+            return _warehouse + "/" + op;
+        }
+        #endregion
+
+        #region 鑾峰彇鍝嶅簲
+        private HttpWebResponse GetResponse(string url, string bodyJson, Dictionary<string, string> header)
+        {
+            byte[] data = Encoding.UTF8.GetBytes(bodyJson);
+
+            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
+            request.Method = WebRequestMethods.Http.Post;
+            request.ContentType = "application/json";
+            request.ContentLength = data.Length;
+            if (header != null)
+            {
+                foreach (var p in header)
+                {
+                    request.Headers.Add(p.Key, p.Value);
+                }
+            }
+
+            using (Stream reqStream = request.GetRequestStream())
+            {
+                reqStream.Write(data, 0, data.Length);
+                reqStream.Close();
+            }
+
+            HttpWebResponse resp = null;
+            try
+            {
+                resp = (HttpWebResponse)request.GetResponse();
+            }
+            catch (WebException ex)
+            {
+                var res = ex.Response;
+                if (res != null)
+                {
+                    string statusBase64 = ((HttpWebResponse)ex.Response).StatusDescription;//base64缂栫爜鍚庣殑鎶ラ敊淇℃伅
+                }
+            }
+            return resp;
+        }
+        #endregion
+
+        #region 淇濆瓨鏂囦欢鍒版湰鍦�
+        /*private void PostDataViaHttpWebRequest(string url, string bodyJson, Dictionary<string, string> header, bool isGetContent)
+        {
+
+            var resp = GetResponse(url, bodyJson, header);
+            var resultJson = string.Empty;
+            Stream stream = File.Open("D://DownloadDocDemo.pdf", FileMode.OpenOrCreate);
+            int packageSize = 1024 * 1024;
+            byte[] buffer = new byte[packageSize];
+            using (stream)
+            {
+                using (Stream outputStream = resp.GetResponseStream())
+                {
+                    int readLength = 0;
+                    while ((readLength = outputStream.Read(buffer, 0, packageSize)) > 0)
+                    {
+                        stream.Write(buffer, 0, readLength);
+                        stream.Flush();
+                    }
+                    outputStream.Close();
+                }
+                stream.Close();
+            }
+        }*/
+        #endregion
+
+        #region 杩斿洖鏂囦欢娴佺粰鍓嶇
+        private Stream PostDataViaHttpWebRequest(string url, string bodyJson, Dictionary<string, string> header, bool isGetContent)
+        {
+
+            var resp = GetResponse(url, bodyJson, header);
+
+            return resp.GetResponseStream(); // 鐩存帴杩斿洖鍝嶅簲娴�
+        }
+        #endregion
+    }
+    #endregion
+}

--
Gitblit v1.9.3