sjz
2025-05-09 597f8e08e6264b2143454e40a7be553d1e8b6df7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using 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 请求的URL
        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
}