/// <summary> /// 从Response获取错误信息 /// </summary> /// <param name="response"></param> /// <returns></returns> public static WfServiceInvokeException FromWebResponse(WebResponse response) { StringBuilder strB = new StringBuilder(); string content = string.Empty; using (Stream stream = response.GetResponseStream()) { StreamReader streamReader = new StreamReader(stream, Encoding.UTF8); content = streamReader.ReadToEnd(); } if (response is HttpWebResponse) { HttpWebResponse hwr = (HttpWebResponse)response; strB.AppendFormat("Status Code: {0}, Description: {1}\n", hwr.StatusCode, hwr.StatusDescription); } strB.AppendLine(content); WfServiceInvokeException result = new WfServiceInvokeException(strB.ToString()); if (response is HttpWebResponse) { HttpWebResponse hwr = (HttpWebResponse)response; result.StatusCode = hwr.StatusCode; result.StatusDescription = hwr.StatusDescription; } return(result); }
/// <summary> /// 调用服务 /// </summary> /// <param name="timeout">请求超时时间,单位毫秒</param> /// <param name="context">上下文参数</param> /// <returns></returns> public object Invoke(TimeSpan timeout, WfApplicationRuntimeParameters context) { try { if (context == null) { context = WfServiceInvoker.InvokeContext; } HttpWebRequest request = GenerateWebRequestObj(timeout, context); try { using (WebResponse response = request.GetResponse()) { using (Stream stream = response.GetResponseStream()) { object result = null; if (stream != null) { StreamReader streamReader = new StreamReader(stream, Encoding.UTF8); string rtnContent = streamReader.ReadToEnd(); result = ParseServiceResultToObject(rtnContent); if (result == null) { result = ExceptionHelper.DoSilentFunc(() => JSONSerializerExecute.DeserializeObject(rtnContent), rtnContent); if (result is WfErrorDTO) { string errorMessage = ((WfErrorDTO)result).ToString() + Environment.NewLine + request.RequestUri.ToString(); throw new WfServiceInvokeException(((WfErrorDTO)result).ToString()); } } } if (result is IDictionary <string, object> ) { foreach (KeyValuePair <string, object> kp in (IDictionary <string, object>)result) { context[kp.Key] = kp.Value; } } if (this._SvcOperationDef.RtnXmlStoreParamName.IsNotEmpty()) { context[this._SvcOperationDef.RtnXmlStoreParamName] = result; } return(result); } } } catch (WebException ex) { if (ex.Response == null) { throw new WfServiceInvokeException(string.Format("调用服务时发生了异常,{0},但无响应内容。HTTP状态为{1}", ex.Message, ex.Status), ex); } else { throw WfServiceInvokeException.FromWebResponse(ex.Response); } } } catch (WebException ex) { throw new WfServiceInvokeException(ex.Message, ex); } }
/// <summary> /// 从Response获取错误信息 /// </summary> /// <param name="response"></param> /// <returns></returns> public static WfServiceInvokeException FromWebResponse(WebResponse response) { StringBuilder strB = new StringBuilder(); string content = string.Empty; using (Stream stream = response.GetResponseStream()) { StreamReader streamReader = new StreamReader(stream, Encoding.UTF8); content = streamReader.ReadToEnd(); } if (response is HttpWebResponse) { HttpWebResponse hwr = (HttpWebResponse)response; strB.AppendFormat("Status Code: {0}, Description: {1}\n", hwr.StatusCode, hwr.StatusDescription); } strB.AppendLine(content); WfServiceInvokeException result = new WfServiceInvokeException(strB.ToString()); if (response is HttpWebResponse) { HttpWebResponse hwr = (HttpWebResponse)response; result.StatusCode = hwr.StatusCode; result.StatusDescription = hwr.StatusDescription; } return result; }