/// <summary> /// 通过Post调用 /// </summary> /// <param name="apiName"></param> /// <param name="form"></param> /// <returns></returns> public ApiValueResult Post(string apiName, string form) { LogRecorderX.BeginStepMonitor("内部API调用" + ToUrl(apiName)); var ctx = string.IsNullOrEmpty(Bearer) ? null : $"Bearer {Bearer}"; LogRecorderX.MonitorTrace(ctx); LogRecorderX.MonitorTrace("Arguments:" + form); var req = (HttpWebRequest)WebRequest.Create(ToUrl(apiName)); req.Method = "POST"; req.ContentType = "application/x-www-form-urlencoded"; req.Headers.Add(HttpRequestHeader.Authorization, ctx); try { using (var rs = req.GetRequestStream()) { var formData = Encoding.UTF8.GetBytes(form); rs.Write(formData, 0, formData.Length); } } catch (Exception ex) { LogRecorderX.Exception(ex); LogRecorderX.EndStepMonitor(); return(ErrorResult(-3)); } using (MonitorScope.CreateScope("Caller Remote")) { return(GetResult(req)); } }
/// <summary> /// 通过Get调用 /// </summary> /// <typeparam name="TResult"></typeparam> /// <param name="apiName"></param> /// <param name="arguments"></param> /// <returns></returns> public ApiResult <TResult> Get <TResult>(string apiName, string arguments) { LogRecorderX.BeginStepMonitor("内部API调用" + ToUrl(apiName)); var ctx = string.IsNullOrEmpty(Bearer) ? null : $"Bearer {Bearer}"; LogRecorderX.MonitorTrace(ctx); LogRecorderX.MonitorTrace("Arguments:" + arguments); if (!string.IsNullOrWhiteSpace(arguments)) { apiName = $"{apiName}?{arguments}"; } var req = (HttpWebRequest)WebRequest.Create(ToUrl(apiName)); req.Method = "GET"; req.ContentType = "application/x-www-form-urlencoded"; req.Headers.Add(HttpRequestHeader.Authorization, ctx); return(GetResult <TResult>(req)); }
/// <summary> /// 取返回值 /// </summary> /// <param name="req"></param> /// <returns></returns> public ApiValueResult GetResult(HttpWebRequest req) { string jsonResult; try { using (var response = req.GetResponse()) { var receivedStream2 = response.GetResponseStream(); if (receivedStream2 == null) { LogRecorderX.EndStepMonitor(); return(ErrorResult(-1, "服务器无返回值")); } jsonResult = new StreamReader(receivedStream2).ReadToEnd(); receivedStream2.Dispose(); response.Close(); } } catch (WebException e3) { try { if (e3.Status != WebExceptionStatus.ProtocolError) { LogRecorderX.EndStepMonitor(); switch (e3.Status) { case WebExceptionStatus.CacheEntryNotFound: return(ErrorResult(-3, "找不到指定的缓存项")); case WebExceptionStatus.ConnectFailure: return(ErrorResult(-3, "在传输级别无法联系远程服务点")); case WebExceptionStatus.ConnectionClosed: return(ErrorResult(-3, "过早关闭连接")); case WebExceptionStatus.KeepAliveFailure: return(ErrorResult(-3, "指定保持活动状态的标头的请求的连接意外关闭")); case WebExceptionStatus.MessageLengthLimitExceeded: return(ErrorResult(-3, "已收到一条消息的发送请求时超出指定的限制或从服务器接收响应")); case WebExceptionStatus.NameResolutionFailure: return(ErrorResult(-3, "名称解析程序服务或无法解析主机名")); case WebExceptionStatus.Pending: return(ErrorResult(-3, "内部异步请求处于挂起状态")); case WebExceptionStatus.PipelineFailure: return(ErrorResult(-3, "该请求是管线请求和连接被关闭之前收到响应")); case WebExceptionStatus.ProxyNameResolutionFailure: return(ErrorResult(-3, "名称解析程序服务无法解析代理服务器主机名")); case WebExceptionStatus.ReceiveFailure: return(ErrorResult(-3, "从远程服务器未收到完整的响应")); case WebExceptionStatus.RequestCanceled: return(ErrorResult(-3, "请求已取消")); case WebExceptionStatus.RequestProhibitedByCachePolicy: return(ErrorResult(-3, "缓存策略不允许该请求")); case WebExceptionStatus.RequestProhibitedByProxy: return(ErrorResult(-3, "由该代理不允许此请求")); case WebExceptionStatus.SecureChannelFailure: return(ErrorResult(-3, "使用 SSL 建立连接时出错")); case WebExceptionStatus.SendFailure: return(ErrorResult(-3, "无法与远程服务器发送一个完整的请求")); case WebExceptionStatus.ServerProtocolViolation: return(ErrorResult(-3, "服务器响应不是有效的 HTTP 响应")); case WebExceptionStatus.Timeout: return(ErrorResult(-3, "请求的超时期限内未不收到任何响应")); case WebExceptionStatus.TrustFailure: LogRecorderX.EndStepMonitor(); return(ErrorResult(-3, "无法验证服务器证书")); default: return(ErrorResult(-3, "发生未知类型的异常")); } } var codes = e3.Message.Split(new[] { '(', ')' }, StringSplitOptions.RemoveEmptyEntries); if (codes.Length == 3 && int.TryParse(codes[1], out var s) && s == 404) { return(ErrorResult(-3, "服务器内部错误", "页面不存在")); } using (var webResponse = e3.Response) { var receivedStream = webResponse.GetResponseStream(); if (receivedStream == null) { LogRecorderX.EndStepMonitor(); return(ErrorResult(-1, "服务器无返回值")); } jsonResult = new StreamReader(receivedStream).ReadToEnd(); receivedStream.Dispose(); webResponse.Close(); } } catch (Exception e) { LogRecorderX.Exception(e); LogRecorderX.EndStepMonitor(); return(ErrorResult(-1, "未知错误", e.Message)); } } catch (Exception e2) { LogRecorderX.Exception(e2); LogRecorderX.EndStepMonitor(); return(ErrorResult(-1, "未知错误", e2.Message)); } LogRecorderX.MonitorTrace(jsonResult); try { if (string.IsNullOrWhiteSpace(jsonResult)) { return(ErrorResult(-1)); } var baseResult = JsonConvert.DeserializeObject <ApiResult>(jsonResult); return((!baseResult.Success) ? ErrorResult(baseResult.Status.ErrorCode, baseResult.Status.ClientMessage) : ApiValueResult.Succees(ReadResultData(jsonResult, "ResultData"))); } catch (Exception e) { LogRecorderX.Exception(e); return(ErrorResult(-1, "未知错误", e.Message)); } }
/// <summary> /// 取返回值 /// </summary> /// <typeparam name="TResult"></typeparam> /// <param name="req"></param> /// <returns></returns> public ApiResult <TResult> GetResult <TResult>(HttpWebRequest req) { string jsonResult; try { using (var webResponse = req.GetResponse()) { var receivedStream2 = webResponse.GetResponseStream(); if (receivedStream2 == null) { LogRecorderX.EndStepMonitor(); return(ApiResult.Error <TResult>(-1, "服务器无返回值")); } jsonResult = new StreamReader(receivedStream2).ReadToEnd(); receivedStream2.Dispose(); webResponse.Close(); } } catch (WebException e) { if (e.Status != WebExceptionStatus.ProtocolError) { LogRecorderX.EndStepMonitor(); switch (e.Status) { case WebExceptionStatus.CacheEntryNotFound: return(ApiResult.Error <TResult>(ErrorCode.RemoteError, "找不到指定的缓存项")); case WebExceptionStatus.ConnectFailure: return(ApiResult.Error <TResult>(ErrorCode.RemoteError, "在传输级别无法联系远程服务点")); case WebExceptionStatus.ConnectionClosed: return(ApiResult.Error <TResult>(ErrorCode.RemoteError, "过早关闭连接")); case WebExceptionStatus.KeepAliveFailure: return(ApiResult.Error <TResult>(ErrorCode.RemoteError, "指定保持活动状态的标头的请求的连接意外关闭")); case WebExceptionStatus.MessageLengthLimitExceeded: return(ApiResult.Error <TResult>(ErrorCode.RemoteError, "已收到一条消息的发送请求时超出指定的限制或从服务器接收响应")); case WebExceptionStatus.NameResolutionFailure: return(ApiResult.Error <TResult>(ErrorCode.RemoteError, "名称解析程序服务或无法解析主机名")); case WebExceptionStatus.Pending: return(ApiResult.Error <TResult>(ErrorCode.RemoteError, "内部异步请求处于挂起状态")); case WebExceptionStatus.PipelineFailure: return(ApiResult.Error <TResult>(ErrorCode.RemoteError, "该请求是管线请求和连接被关闭之前收到响应")); case WebExceptionStatus.ProxyNameResolutionFailure: return(ApiResult.Error <TResult>(ErrorCode.RemoteError, "名称解析程序服务无法解析代理服务器主机名")); case WebExceptionStatus.ReceiveFailure: return(ApiResult.Error <TResult>(ErrorCode.RemoteError, "从远程服务器未收到完整的响应")); case WebExceptionStatus.RequestCanceled: return(ApiResult.Error <TResult>(ErrorCode.RemoteError, "请求已取消")); case WebExceptionStatus.RequestProhibitedByCachePolicy: return(ApiResult.Error <TResult>(ErrorCode.RemoteError, "缓存策略不允许该请求")); case WebExceptionStatus.RequestProhibitedByProxy: return(ApiResult.Error <TResult>(ErrorCode.RemoteError, "由该代理不允许此请求")); case WebExceptionStatus.SecureChannelFailure: return(ApiResult.Error <TResult>(ErrorCode.RemoteError, "使用 SSL 建立连接时出错")); case WebExceptionStatus.SendFailure: return(ApiResult.Error <TResult>(ErrorCode.RemoteError, "无法与远程服务器发送一个完整的请求")); case WebExceptionStatus.ServerProtocolViolation: return(ApiResult.Error <TResult>(ErrorCode.RemoteError, "服务器响应不是有效的 HTTP 响应")); case WebExceptionStatus.Timeout: return(ApiResult.Error <TResult>(ErrorCode.RemoteError, "请求的超时期限内未不收到任何响应")); case WebExceptionStatus.TrustFailure: return(ApiResult.Error <TResult>(ErrorCode.RemoteError, "无法验证服务器证书")); default: return(ApiResult.Error <TResult>(ErrorCode.RemoteError, "发生未知类型的异常")); } } using (var response = e.Response) { var receivedStream = response.GetResponseStream(); if (receivedStream == null) { LogRecorderX.EndStepMonitor(); return(ApiResult.Error <TResult>(-1, "服务器无返回值")); } jsonResult = new StreamReader(receivedStream).ReadToEnd(); receivedStream.Dispose(); response.Close(); } } catch (Exception ex) { LogRecorderX.Exception(ex); LogRecorderX.EndStepMonitor(); return(ApiResult.Error <TResult>(ErrorCode.RemoteError)); } LogRecorderX.MonitorTrace(jsonResult); try { return(string.IsNullOrWhiteSpace(jsonResult) ? ApiResult.Error <TResult>(-1) : JsonConvert.DeserializeObject <ApiResult <TResult> >(jsonResult)); } catch (Exception ex2) { LogRecorderX.Exception(ex2); return(ApiResult.Error <TResult>(-1)); } finally { LogRecorderX.EndStepMonitor(); } }