Ejemplo n.º 1
0
        private void GetValidWebServiceUrl(bool throwException)
        {
            lock (_lock)
            {
                bool             updateWebServiceSettingsWhenAllServicesAreDown = true;
                WebBrowserClient webClient     = new WebBrowserClient();
                WebServiceUrl    webServiceUrl = GetCurrentWebServiceUrl();

                if (_currentWebServiceUrl != null && (DateTime.Now - _lastPingDateTime).TotalSeconds <= 15)
                {
                    return;
                }

                int retries = 1;
TryAgain:
                try
                {
                    if (webServiceUrl != null && PingWebService(webServiceUrl.Url, false))
                    {
                        _lastPingDateTime     = DateTime.Now;
                        _currentWebServiceUrl = webServiceUrl;
                    }
                    else
                    {
                        throw new Exception(Constants.Message.TRY_NEXT_URL);
                    }
                }
                catch (Exception ex)
                {
                    if (ex.Message.Equals(Constants.Message.TRY_NEXT_URL))
                    {
                        if (retries < GetNumberOfWebServiceUrl())
                        {
                            retries++;
                            webServiceUrl = GetNextWebServiceUrl();
                            goto TryAgain;
                        }
                        else if (updateWebServiceSettingsWhenAllServicesAreDown)
                        {
                            updateWebServiceSettingsWhenAllServicesAreDown = false;
                            WebServiceSettings tempWebServiceSettings = GetLatestWebServiceSettings(false);
                            if (tempWebServiceSettings != null && _webServiceSettings.SettingId != tempWebServiceSettings.SettingId)
                            {
                                _webServiceSettings.SettingId         = tempWebServiceSettings.SettingId;
                                _webServiceSettings.WebServiceUrlList = tempWebServiceSettings.WebServiceUrlList;
                                _currentWebServiceUrlIndex            = 0;
                                webServiceUrl = GetCurrentWebServiceUrl();
                                retries       = 1;
                                goto TryAgain;
                            }
                        }
                        if (throwException)
                        {
                            throw new Exception(Constants.Message.WEBSERVICES_ARE_DOWN);
                        }
                    }
                    else if (throwException)
                    {
                        throw ex;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public async System.Threading.Tasks.Task <RawResponse> GetResponse(JObject source)
        {
            string        fullUrl     = WebServiceUrl.TrimEnd(new char[] { '/', ' ' });
            StringBuilder bodyBuilder = new StringBuilder();
            JToken        requestJson = source.SelectToken("$.body");
            //直接将客户发送来的body中的json作为web service的CDATA传递,
            // 不验证它与服务api定义的符合性。
            string data = requestJson?.ToString();

            XNamespace xsi  = "http://www.w3.org/2001/XMLSchema-instance";
            XNamespace test = "http://www.w3.org/2001/XMLSchema";
            XNamespace soap = "http://schemas.xmlsoap.org/soap/envelope/";

            var document = new XDocument(
                new XDeclaration("1.0", "utf-8", null),
                new XElement(soap + "Envelope",
                             new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName),
                             new XAttribute(XNamespace.Xmlns + "test", test.NamespaceName),
                             new XAttribute(XNamespace.Xmlns + "soap", soap.NamespaceName),
                             new XElement(soap + "Body",
                                          new XElement(test + EndPoint, //"syncPsndoc",
                                                       new XElement("string",
                                                                    new XCData(data)
                                                                    )
                                                       )
                                          )
                             )
                );

            string postXml = document.Declaration + Environment.NewLine + document.ToString();

            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));
        }