예제 #1
0
        public async System.Threading.Tasks.Task <RawResponse> GetResponse(JObject source)
        {
            string        fullUrl     = ServiceAddress.TrimEnd(new char[] { '/', ' ' });
            StringBuilder bodyBuilder = new StringBuilder();
            //直接将客户发送来的body中的json作为SOAP Body传递,
            // 不验证它与服务api定义的符合性。

            string postXml = generatePostXml(source);

            var client  = new RestClient(fullUrl);
            var request = new RestRequest(Method.POST);

            request.Timeout          = 1800000; //1800秒=30分钟
            request.ReadWriteTimeout = 1800000; //1800秒=30分钟
            if (fullUrl.StartsWith("https", StringComparison.OrdinalIgnoreCase))
            {
                client.RemoteCertificateValidationCallback = (s, c, e, t) => true;
            }
            request.AddHeader("Content-Type", "application/xml");
            request.AddParameter("application/xml", postXml, ParameterType.RequestBody);
            IRestResponse response = await client.ExecuteAsync(request);

            if (!response.IsSuccessful)
            {
                if (response.ErrorException != null)
                {
                    throw response.ErrorException;
                }
                else if (!string.IsNullOrWhiteSpace(response.ErrorMessage))
                {
                    throw new Exception(response.ErrorMessage);
                }
                else
                {
                    StringBuilder errorBuilder = new StringBuilder();
                    errorBuilder.AppendLine($"向 {fullUrl} 发送数据出现错误。");
                    errorBuilder.AppendLine($"StatusCode={response.StatusCode}");
                    errorBuilder.AppendLine($"Response:{response.Content}");
                    errorBuilder.AppendLine("Xml Parameter Posted Is:");
                    errorBuilder.AppendLine(postXml);
                    throw new Exception(errorBuilder.ToString());
                }
            }
            return(new RawResponse(response, fullUrl));
        }