/// <summary> /// Makes a request to API service /// </summary> /// <param name="apiCallHandler"></param> /// <returns></returns> public string MakeRequestUsing(IAPICallPreHandler apiCallHandler) { string responseString = string.Empty; string uri = apiCallHandler.GetEndpoint(); Dictionary <string, string> headers = apiCallHandler.GetHeaderMap(); string payload = apiCallHandler.GetPayload(); //Constructing HttpWebRequest object ConnectionManager connMngr = ConnectionManager.Instance; HttpWebRequest httpRequest = connMngr.GetConnection(this.config, uri); httpRequest.Method = RequestMethod; if (headers != null && headers.ContainsKey(BaseConstants.ContentTypeHeader)) { httpRequest.ContentType = headers[BaseConstants.ContentTypeHeader].Trim(); headers.Remove(BaseConstants.ContentTypeHeader); } if (headers != null && headers.ContainsKey(BaseConstants.UserAgentHeader)) { httpRequest.UserAgent = headers[BaseConstants.UserAgentHeader].Trim(); headers.Remove(BaseConstants.UserAgentHeader); } foreach (KeyValuePair <string, string> header in headers) { httpRequest.Headers.Add(header.Key, header.Value); } foreach (string headerName in httpRequest.Headers) { logger.DebugFormat(headerName + ":" + httpRequest.Headers[headerName]); } if (apiCallHandler.GetCredential() is CertificateCredential) { CertificateCredential certCredential = (CertificateCredential)apiCallHandler.GetCredential(); //Load the certificate into an X509Certificate2 object. if (((CertificateCredential)certCredential).PrivateKeyPassword.Trim() == string.Empty) { x509 = new X509Certificate2(((CertificateCredential)certCredential).CertificateFile); } else { x509 = new X509Certificate2(((CertificateCredential)certCredential).CertificateFile, ((CertificateCredential)certCredential).PrivateKeyPassword); } httpRequest.ClientCertificates.Add(x509); } HttpConnection connectionHttp = new HttpConnection(config); string response = connectionHttp.Execute(payload, httpRequest); return(response); }
/// <summary> /// Makes a request to API service /// </summary> /// <param name="apiCallHandler"></param> /// <returns></returns> public string MakeRequestUsing(IAPICallPreHandler apiCallHandler) { string responseString = string.Empty; string uri = apiCallHandler.GetEndpoint(); Dictionary <string, string> headers = apiCallHandler.GetHeaderMap(); string payload = apiCallHandler.GetPayload(); //Constructing HttpWebRequest object ConnectionManager connMngr = ConnectionManager.Instance; HttpWebRequest httpRequest = connMngr.GetConnection(this.config, uri); httpRequest.Method = RequestMethod; if (headers != null && headers.ContainsKey(BaseConstants.ContentTypeHeader)) { httpRequest.ContentType = headers[BaseConstants.ContentTypeHeader].Trim(); headers.Remove(BaseConstants.ContentTypeHeader); } if (headers != null && headers.ContainsKey(BaseConstants.UserAgentHeader)) { httpRequest.UserAgent = headers[BaseConstants.UserAgentHeader].Trim(); headers.Remove(BaseConstants.UserAgentHeader); } foreach (KeyValuePair <string, string> header in headers) { httpRequest.Headers.Add(header.Key, header.Value); } foreach (string headerName in httpRequest.Headers) { logger.DebugFormat(headerName + ":" + httpRequest.Headers[headerName]); } if (apiCallHandler.GetCredential() is CertificateCredential) { CertificateCredential certCredential = (CertificateCredential)apiCallHandler.GetCredential(); System.Exception x509LoadFromFileException = null; X509Certificate2 x509 = null; try { //Load the certificate into an X509Certificate2 object. if (string.IsNullOrEmpty(certCredential.PrivateKeyPassword.Trim())) { x509 = new X509Certificate2(certCredential.CertificateFile); } else { x509 = new X509Certificate2(certCredential.CertificateFile, certCredential.PrivateKeyPassword); } } catch (System.Exception ex) { x509LoadFromFileException = ex; } // If we failed to load the certificate from the specified file, // then try loading it from the certificates store using the provided UserName. if (x509 == null) { // Start by checking the local machine store. x509 = this.GetX509CertificateForUserName(certCredential.UserName, StoreLocation.LocalMachine); } // If the certificate couldn't be found in the LM store, check // the current user store. if (x509 == null) { x509 = this.GetX509CertificateForUserName(certCredential.UserName, StoreLocation.CurrentUser); } // If the certificate still hasn't been loaded by this point, // then it means it failed to be loaded from a file and was not // found in any of the certificate stores. if (x509 == null) { throw new PayPalException("Failed to load the certificate file", x509LoadFromFileException); } httpRequest.ClientCertificates.Add(x509); } HttpConnection connectionHttp = new HttpConnection(config); string response = connectionHttp.Execute(payload, httpRequest); return(response); }
/// <summary> /// Makes a request to API service /// </summary> /// <param name="apiCallHandler"></param> /// <returns></returns> public string MakeRequestUsing(IAPICallPreHandler apiCallHandler) { string responseString = string.Empty; string uri = apiCallHandler.GetEndPoint(); Dictionary<string, string> headers = apiCallHandler.GetHeaderMap(); string payLoad = apiCallHandler.GetPayLoad(); // Constructing HttpWebRequest object ConnectionManager connMngr = ConnectionManager.Instance; HttpWebRequest httpRequest = connMngr.GetConnection(this.config, uri); httpRequest.Method = RequestMethod; foreach (KeyValuePair<string, string> header in headers) { httpRequest.Headers.Add(header.Key, header.Value); } if (logger.IsDebugEnabled) { foreach (string headerName in httpRequest.Headers) { logger.Debug(headerName + ":" + httpRequest.Headers[headerName]); } } // Adding payLoad to HttpWebRequest object using (StreamWriter myWriter = new StreamWriter(httpRequest.GetRequestStream())) { myWriter.Write(payLoad); logger.Debug(payLoad); } if (apiCallHandler.GetCredential() is CertificateCredential) { CertificateCredential certCredential = (CertificateCredential)apiCallHandler.GetCredential(); // Load the certificate into an X509Certificate2 object. if (((CertificateCredential)certCredential).PrivateKeyPassword.Trim() == string.Empty) { x509 = new X509Certificate2(((CertificateCredential)certCredential).CertificateFile); } else { x509 = new X509Certificate2(((CertificateCredential)certCredential).CertificateFile, ((CertificateCredential)certCredential).PrivateKeyPassword); } httpRequest.ClientCertificates.Add(x509); } // Fire request. Retry if configured to do so int numRetries = (this.config.ContainsKey(BaseConstants.HTTP_CONNECTION_RETRY_CONFIG)) ? Convert.ToInt32(config[BaseConstants.HTTP_CONNECTION_RETRY_CONFIG]) : 0; int retries = 0; do { try { // Calling the plaftform API web service and getting the response using (WebResponse response = httpRequest.GetResponse()) { using (StreamReader sr = new StreamReader(response.GetResponseStream())) { responseString = sr.ReadToEnd(); logger.Debug("Service response"); logger.Debug(responseString); return responseString; } } } // Server responses in the range of 4xx and 5xx throw a WebException catch (WebException we) { HttpStatusCode statusCode = ((HttpWebResponse)we.Response).StatusCode; logger.Info("Got " + statusCode.ToString() + " response from server"); if (!RequiresRetry(we)) { throw new ConnectionException("Invalid HTTP response " + we.Message); } } catch(System.Exception ex) { throw ex; } } while (retries++ < numRetries); throw new ConnectionException("Invalid HTTP response"); }
/// <summary> /// Makes a request to API service /// </summary> /// <param name="apiCallHandler"></param> /// <returns></returns> public string MakeRequestUsing(IAPICallPreHandler apiCallHandler) { string responseString = string.Empty; string uri = apiCallHandler.GetEndpoint(); Dictionary<string, string> headers = apiCallHandler.GetHeaderMap(); string payload = apiCallHandler.GetPayload(); //Constructing HttpWebRequest object ConnectionManager connMngr = ConnectionManager.Instance; HttpWebRequest httpRequest = connMngr.GetConnection(this.config, uri); httpRequest.Method = RequestMethod; if (headers != null && headers.ContainsKey(BaseConstants.ContentTypeHeader)) { httpRequest.ContentType = headers[BaseConstants.ContentTypeHeader].Trim(); headers.Remove(BaseConstants.ContentTypeHeader); } if (headers != null && headers.ContainsKey(BaseConstants.UserAgentHeader)) { httpRequest.UserAgent = headers[BaseConstants.UserAgentHeader].Trim(); headers.Remove(BaseConstants.UserAgentHeader); } foreach (KeyValuePair<string, string> header in headers) { httpRequest.Headers.Add(header.Key, header.Value); } foreach (string headerName in httpRequest.Headers) { logger.DebugFormat(headerName + ":" + httpRequest.Headers[headerName]); } if (apiCallHandler.GetCredential() is CertificateCredential) { CertificateCredential certCredential = (CertificateCredential)apiCallHandler.GetCredential(); System.Exception x509LoadFromFileException = null; X509Certificate2 x509 = null; try { //Load the certificate into an X509Certificate2 object. if (string.IsNullOrEmpty(certCredential.PrivateKeyPassword.Trim())) { x509 = new X509Certificate2(certCredential.CertificateFile); } else { x509 = new X509Certificate2(certCredential.CertificateFile, certCredential.PrivateKeyPassword); } } catch (System.Exception ex) { x509LoadFromFileException = ex; } // If we failed to load the certificate from the specified file, // then try loading it from the certificates store using the provided UserName. if (x509 == null) { // Start by checking the local machine store. x509 = this.GetX509CertificateForUserName(certCredential.UserName, StoreLocation.LocalMachine); } // If the certificate couldn't be found in the LM store, check // the current user store. if (x509 == null) { x509 = this.GetX509CertificateForUserName(certCredential.UserName, StoreLocation.CurrentUser); } // If the certificate still hasn't been loaded by this point, // then it means it failed to be loaded from a file and was not // found in any of the certificate stores. if (x509 == null) { throw new PayPalException("Failed to load the certificate file", x509LoadFromFileException); } httpRequest.ClientCertificates.Add(x509); } HttpConnection connectionHttp = new HttpConnection(config); string response = connectionHttp.Execute(payload, httpRequest); return response; }
/// <summary> /// Makes a request to API service /// </summary> /// <param name="apiCallHandler"></param> /// <returns></returns> public string MakeRequestUsing(IAPICallPreHandler apiCallHandler) { string responseString = string.Empty; string uri = apiCallHandler.GetEndpoint(); Dictionary<string, string> headers = apiCallHandler.GetHeaderMap(); string payload = apiCallHandler.GetPayload(); //Constructing HttpWebRequest object ConnectionManager connMngr = ConnectionManager.Instance; HttpWebRequest httpRequest = connMngr.GetConnection(this.config, uri); httpRequest.Method = RequestMethod; if (headers != null && headers.ContainsKey(BaseConstants.ContentTypeHeader)) { httpRequest.ContentType = headers[BaseConstants.ContentTypeHeader].Trim(); headers.Remove(BaseConstants.ContentTypeHeader); } if (headers != null && headers.ContainsKey(BaseConstants.UserAgentHeader)) { httpRequest.UserAgent = headers[BaseConstants.UserAgentHeader].Trim(); headers.Remove(BaseConstants.UserAgentHeader); } foreach (KeyValuePair<string, string> header in headers) { httpRequest.Headers.Add(header.Key, header.Value); } foreach (string headerName in httpRequest.Headers) { logger.DebugFormat(headerName + ":" + httpRequest.Headers[headerName]); } if (apiCallHandler.GetCredential() is CertificateCredential) { CertificateCredential certCredential = (CertificateCredential)apiCallHandler.GetCredential(); //Load the certificate into an X509Certificate2 object. if (((CertificateCredential)certCredential).PrivateKeyPassword.Trim() == string.Empty) { x509 = new X509Certificate2(((CertificateCredential)certCredential).CertificateFile); } else { x509 = new X509Certificate2(((CertificateCredential)certCredential).CertificateFile, ((CertificateCredential)certCredential).PrivateKeyPassword); } httpRequest.ClientCertificates.Add(x509); } HttpConnection connectionHttp = new HttpConnection(config); string response = connectionHttp.Execute(payload, httpRequest); return response; }
/// <summary> /// Makes a request to API service /// </summary> /// <param name="apiCallHandler"></param> /// <returns></returns> public string MakeRequestUsing(IAPICallPreHandler apiCallHandler) { string responseString = string.Empty; string uri = apiCallHandler.GetEndPoint(); Dictionary <string, string> headers = apiCallHandler.GetHeaderMap(); string payLoad = apiCallHandler.GetPayLoad(); // Constructing HttpWebRequest object ConnectionManager connMngr = ConnectionManager.Instance; HttpWebRequest httpRequest = connMngr.GetConnection(this.config, uri); httpRequest.Method = RequestMethod; foreach (KeyValuePair <string, string> header in headers) { httpRequest.Headers.Add(header.Key, header.Value); } if (logger.IsDebugEnabled) { foreach (string headerName in httpRequest.Headers) { logger.Debug(headerName + ":" + httpRequest.Headers[headerName]); } } // Adding payLoad to HttpWebRequest object using (StreamWriter myWriter = new StreamWriter(httpRequest.GetRequestStream())) { myWriter.Write(payLoad); logger.Debug(payLoad); } if (apiCallHandler.GetCredential() is CertificateCredential) { CertificateCredential certCredential = (CertificateCredential)apiCallHandler.GetCredential(); // Load the certificate into an X509Certificate2 object. if (((CertificateCredential)certCredential).PrivateKeyPassword.Trim() == string.Empty) { x509 = new X509Certificate2(((CertificateCredential)certCredential).CertificateFile); } else { x509 = new X509Certificate2(((CertificateCredential)certCredential).CertificateFile, ((CertificateCredential)certCredential).PrivateKeyPassword); } httpRequest.ClientCertificates.Add(x509); } // Fire request. Retry if configured to do so int numRetries = (this.config.ContainsKey(BaseConstants.HTTP_CONNECTION_RETRY_CONFIG)) ? Convert.ToInt32(config[BaseConstants.HTTP_CONNECTION_RETRY_CONFIG]) : 0; int retries = 0; do { try { // Calling the plaftform API web service and getting the response using (WebResponse response = httpRequest.GetResponse()) { using (StreamReader sr = new StreamReader(response.GetResponseStream())) { responseString = sr.ReadToEnd(); logger.Debug("Service response"); logger.Debug(responseString); return(responseString); } } } // Server responses in the range of 4xx and 5xx throw a WebException catch (WebException we) { HttpStatusCode statusCode = ((HttpWebResponse)we.Response).StatusCode; logger.Info("Got " + statusCode.ToString() + " response from server"); if (!RequiresRetry(we)) { throw new ConnectionException("Invalid HTTP response " + we.Message); } } catch (System.Exception ex) { throw ex; } } while (retries++ < numRetries); throw new ConnectionException("Invalid HTTP response"); }