Example #1
0
        public static T SendReceiveVeisRequest <T>(VeisConfig config, string messageId, VeisRequest request)
        {
            string reqBody = JsonHelper.Serialize(request);

            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;
                    if (config.VeisConfiguration.SvcConfigInfo.SvcLOBServiceUrl.EndsWith("/"))
                    {
                        uri = config.VeisConfiguration.SvcConfigInfo.SvcLOBServiceUrl + messageId;
                    }
                    else
                    {
                        uri = config.VeisConfiguration.SvcConfigInfo.SvcLOBServiceUrl + "/" + messageId;
                    }
                    client.AddAuthHeader(config.VeisConfiguration.CRMAuthInfo);
                    Console.WriteLine("Auth Header: " + client.Headers[HttpRequestHeader.Authorization]);
                    client.Headers[HttpRequestHeader.ContentType] = "application/json";
                    string subKeys = config.VeisConfiguration.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.UploadString(uri, reqBody);
                    if (typeof(T) == typeof(string))
                    {
                        return((T)(object)response);
                    }
                    else
                    {
                        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} Request: {reqBody}", exception);
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(ex.ToString());
            }
        }
Example #2
0
 public static T SendReceiveVeisRequest <T>(IPluginExecutionContext localContext, VeisConfig config, string messageId, VeisRequest request)
 {
     //localContext.TracingService.Trace("Sending Request");
     return(SendReceiveVeisRequest <T>(config, messageId, request));
 }