| | |
| | | } |
| | | |
| | | |
| | | public string GetOaKqInfo(Uri url, string token, string userId) |
| | | { |
| | | try |
| | | { |
| | | ServicePointManager.ServerCertificateValidationCallback += (s, cert, chain, sslPolicyErrors) => true; |
| | | ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; |
| | | |
| | | var request = WebRequest.Create(url) as HttpWebRequest; |
| | | request.Method = "POST"; |
| | | request.Timeout = 20000; // 20秒超时 |
| | | request.ContentType = "application/x-www-form-urlencoded; charset=utf-8"; // 明确指定编码 |
| | | |
| | | // 添加调试头信息 |
| | | request.Headers.Add("appid", APPID); |
| | | request.Headers.Add("token", token); |
| | | request.Headers.Add("userId", userId); |
| | | |
| | | // 构建带时区的日期参数 |
| | | var postData = new StringBuilder(); |
| | | var dateParam = DateTime.Now.AddDays(-1).ToString("yyyy-MM-ddTHH:mm:sszzz"); |
| | | postData.AppendFormat("KQSDATE={0}&KQEDATE={1}", |
| | | Uri.EscapeDataString(dateParam), |
| | | Uri.EscapeDataString(dateParam)); |
| | | |
| | | byte[] byteData = Encoding.UTF8.GetBytes(postData.ToString()); // 改用UTF8编码 |
| | | |
| | | // 更完善的请求写入 |
| | | using (var stream = request.GetRequestStream()) |
| | | { |
| | | stream.Write(byteData, 0, byteData.Length); |
| | | } |
| | | |
| | | // 处理HTTP错误状态码 |
| | | using (var response = request.GetResponse() as HttpWebResponse) |
| | | { |
| | | if (response.StatusCode != HttpStatusCode.OK) |
| | | { |
| | | throw new WebException($"服务器返回错误状态码: {(int)response.StatusCode} {response.StatusDescription}"); |
| | | } |
| | | |
| | | using (var stream = response.GetResponseStream()) |
| | | using (var reader = new StreamReader(stream, Encoding.UTF8)) |
| | | { |
| | | return reader.ReadToEnd(); |
| | | } |
| | | } |
| | | } |
| | | catch (WebException ex) when (ex.Response is HttpWebResponse response) |
| | | { |
| | | // 记录详细错误信息 |
| | | var errorStream = response.GetResponseStream(); |
| | | using (var reader = new StreamReader(errorStream)) |
| | | { |
| | | string errorDetails = reader.ReadToEnd(); |
| | | throw new Exception($"请求失败 [Status: {response.StatusCode}]: {errorDetails}", ex); |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | throw new Exception($"请求异常: {ex.Message}", ex); |
| | | } |
| | | } |
| | | |
| | | |
| | | //public dynamic SumbitIQCToOA(dynamic queryObj) |
| | | //public string GetOaKqInfo(Uri url, string token, string userId) |
| | | //{ |
| | | // try |
| | | // { |
| | |
| | | // throw new Exception($"请求异常: {ex.Message}", ex); |
| | | // } |
| | | //} |
| | | |
| | | |
| | | public dynamic SubmitIQCToOA(dynamic query) |
| | | { |
| | | try |
| | | { |
| | | // 参数校验 |
| | | if (query == null) throw new ArgumentNullException(nameof(query), "参数对象不能为null"); |
| | | //if (query.workflowId == null) throw new ArgumentException("workflowId不能为空"); |
| | | //if (query.mainData == null) throw new ArgumentException("mainData不能为空"); |
| | | |
| | | // 转换动态参数 |
| | | //var mainDataDict = ((IEnumerable<KeyValuePair<string, object>>)query.mainData) |
| | | // .ToDictionary(kvp => kvp.Key, kvp => kvp.Value); |
| | | |
| | | Dictionary<string, object> mainDataDict = null; |
| | | |
| | | Dictionary<string, object> detailDataDict = null; |
| | | //if (query.detailData != null) |
| | | //{ |
| | | // detailDataDict = ((IEnumerable<KeyValuePair<string, object>>)query.detailData) |
| | | // .ToDictionary(kvp => kvp.Key, kvp => kvp.Value); |
| | | //} |
| | | |
| | | // 加密处理 |
| | | var userId = "FRadmin"; |
| | | var encryptedUserId = RsaHelper.Encrypt(userId, SPK); |
| | | string strToken = GetToken(); |
| | | |
| | | // 构建有效测试数据 |
| | | mainDataDict = new Dictionary<string, object> |
| | | { |
| | | ["KQSDATE"] = DateTime.Now.AddDays(-1).ToString("yyyy-MM-ddTHH:mm:sszzz"), |
| | | ["KQEDATE"] = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz") |
| | | }; |
| | | |
| | | // 调用方法增加userid请求头 |
| | | var result = SubmitWorkflowRequest( |
| | | token: strToken, |
| | | workflowId: 379, |
| | | mainData: mainDataDict, |
| | | requestName: "MES->OA测试流程标题", |
| | | otherParams:null, |
| | | remark:"Test", |
| | | requestLevel:"", |
| | | encryptedUserId: encryptedUserId // 新增参数 |
| | | ); |
| | | return result; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | // 更详细的错误处理 |
| | | return new { |
| | | status = -1, |
| | | message = $"完整错误信息:{ex.ToString()}" // 显示完整堆栈 |
| | | }; |
| | | } |
| | | } |
| | | |
| | | // 修改方法签名 |
| | | public dynamic SubmitWorkflowRequest( |
| | | string token, |
| | | int workflowId, |
| | | Dictionary<string, object> mainData, |
| | | string encryptedUserId, // 用户id |
| | | Dictionary<string, object> detailData = null, |
| | | Dictionary<string, object> otherParams = null, |
| | | string remark = "", |
| | | string requestLevel = "", |
| | | string requestName = "默认流程标题") |
| | | { |
| | | try |
| | | { |
| | | ServicePointManager.ServerCertificateValidationCallback += (s, cert, chain, sslPolicyErrors) => true; |
| | | ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; |
| | | |
| | | var url = new Uri($"{HOST}/api/workflow/paService/doCreateRequest"); |
| | | var request = WebRequest.Create(url) as HttpWebRequest; |
| | | request.Method = "POST"; |
| | | request.Timeout = 20000; |
| | | request.ContentType = "application/json; charset=utf-8"; // 修改为JSON格式 |
| | | |
| | | // 构建请求头 |
| | | request.Headers.Add("appid", APPID); |
| | | request.Headers.Add("token", token); |
| | | request.Headers.Add("Content-Type", APPID); |
| | | request.Headers.Add("userId", encryptedUserId); |
| | | // 假设需要加密的userid从其他地方获取,这里需要补充获取逻辑 |
| | | |
| | | // 构建请求体 |
| | | var requestBody = new Dictionary<string, object> |
| | | { |
| | | //["userId"] = "1268", |
| | | ["workflowId"] = workflowId, |
| | | ["mainData"] = mainData, |
| | | ["detailData"] = detailData ?? new Dictionary<string, object>(), |
| | | ["otherParams"] = otherParams ?? new Dictionary<string, object>(), |
| | | ["remark"] = remark, |
| | | ["requestLevel"] = requestLevel, |
| | | ["requestName"] = requestName |
| | | }; |
| | | |
| | | // 序列化请求体 |
| | | var jsonBody = JsonConvert.SerializeObject(requestBody); |
| | | byte[] byteData = Encoding.UTF8.GetBytes(jsonBody); |
| | | |
| | | // 发送请求 |
| | | using (var stream = request.GetRequestStream()) |
| | | { |
| | | stream.Write(byteData, 0, byteData.Length); |
| | | } |
| | | |
| | | // 处理响应 |
| | | using (var response = request.GetResponse() as HttpWebResponse) |
| | | { |
| | | using (var stream = response.GetResponseStream()) |
| | | using (var reader = new StreamReader(stream, Encoding.UTF8)) |
| | | { |
| | | var responseJson = reader.ReadToEnd(); |
| | | var result = JsonConvert.DeserializeObject<dynamic>(responseJson); |
| | | |
| | | // 添加状态码检查 |
| | | if (response.StatusCode != HttpStatusCode.OK || result.code != "SUCCESS") |
| | | { |
| | | throw new Exception($"流程提交失败:{result.errMsg}"); |
| | | } |
| | | |
| | | return new |
| | | { |
| | | code = result.code, |
| | | data = result.data, |
| | | errMsg = result.errMsg |
| | | }; |
| | | } |
| | | } |
| | | } |
| | | catch (WebException ex) when (ex.Response is HttpWebResponse response) |
| | | { |
| | | using (var stream = response.GetResponseStream()) |
| | | using (var reader = new StreamReader(stream)) |
| | | { |
| | | var errorDetails = reader.ReadToEnd(); |
| | | throw new Exception($"请求失败 [Status: {response.StatusCode}]: {errorDetails}"); |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | throw new Exception($"流程提交异常: {ex.Message}"); |
| | | } |
| | | } |
| | | } |