Пример #1
0
        public static T ReceiveVeisRequest <T>(VeisConfiguration config, string messageId)
        {
            try
            {
                if ((System.Net.ServicePointManager.SecurityProtocol & SecurityProtocolType.Tls12) == 0)
                {
                    System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
                }

                using (WebClient client = new WebClient())
                {
                    string uri = string.Empty;
                    if ((config.SvcConfigInfo.SvcLOBServiceUrl.EndsWith("/") && (!messageId.StartsWith("/"))) || ((!config.SvcConfigInfo.SvcLOBServiceUrl.EndsWith("/")) && (messageId.StartsWith("/"))))
                    {
                        uri = config.SvcConfigInfo.SvcLOBServiceUrl + messageId;
                    }
                    else if (config.SvcConfigInfo.SvcLOBServiceUrl.EndsWith("/") && (messageId.StartsWith("/")))
                    {
                        uri = config.SvcConfigInfo.SvcLOBServiceUrl + messageId.TrimStart('/');
                    }
                    else if ((!config.SvcConfigInfo.SvcLOBServiceUrl.EndsWith("/")) && (!messageId.StartsWith("/")))
                    {
                        uri = config.SvcConfigInfo.SvcLOBServiceUrl + "/" + messageId;
                    }

                    client.AddAuthHeader(config.CRMAuthInfo);
                    Console.WriteLine("Auth Header: " + client.Headers[HttpRequestHeader.Authorization]);
                    client.Headers[HttpRequestHeader.ContentType] = "application/json";
                    string subKeys = config.SvcConfigInfo.ApimSubscriptionKey;
                    if (subKeys.Length > 0)
                    {
                        string[] headers = subKeys.Split('|');
                        for (int i = 0; i < headers.Length; i = i + 2)
                        {
                            client.Headers.Add(headers[i], headers[i + 1]);
                        }
                    }
                    string response = client.DownloadString(uri);
                    return(JsonHelper.Deserialize <T>(response));
                }
            }

            catch (WebException exception)
            {
                string callResponse = string.Empty;
                if (exception.Response != null)
                {
                    using (StreamReader reader = new StreamReader(exception.Response.GetResponseStream()))
                    {
                        callResponse = reader.ReadToEnd();
                    }
                    exception.Response.Close();
                }
                if (exception.Status == WebExceptionStatus.Timeout)
                {
                    throw new Exception("The timeout elapsed while attempting to issue the request.", exception);
                }
                throw new Exception($"A Web exception occurred while attempting to issue the request. {exception.Message}: {callResponse}", exception);
            }
        }