/// <summary> /// This is a special method for authentication /// but the request and response are type independent as it is /// in the generic EVHttpClient class /// </summary> /// <typeparam name="TResponse">Response Type</typeparam> /// <param name="uri">Uri of stubbed filename/Rest webservice</param> /// <param name="httpClient">HttpClient object</param> /// <param name="sessionId">SessionId object</param> /// <returns>TResponse TypeParam</returns> public static object Authenticate(string uri, HttpClient httpClient, Type entityType, ref string sessionId) { const string SESSION_ID = "ASP.Net_SessionId"; string xml = string.Empty; HttpResponseMessage httpResp = null; HeaderValues <Cookie> myCookies; httpResp = httpClient.Post(uri, HttpContentExtensions.CreateXmlSerializable(string.Empty)); if (!httpResp.IsStatusIsSuccessful()) { throw new EVException().AddHttpResponse(httpResp); } // -- read the content as an xml string xml = httpResp.Content.ReadAsString(); // -- get the cookies from the response headers myCookies = httpResp.Headers.SetCookie; if (myCookies != null) { foreach (Cookie var in myCookies.Where(var => var.ContainsKey(SESSION_ID))) { // -- set the session ID sessionId = var[SESSION_ID]; break; } } return(XmlUtility.DeserializeObject(xml, entityType)); }
/// <summary> /// A Get method to fetch the stream from the URI /// </summary> /// <typeparam name="TRequest">The Request Object to pass to</typeparam> /// <param name="httpMethod">HTTP Method</param> /// <param name="uri">The Source URL from where Stream can be downloaded</param> /// <param name="request">The input object which should be passed as part of request</param> /// <returns></returns> public Stream GetStream <TRequest>(HttpMethod httpMethod, string uri, TRequest request) where TRequest : class { HttpClient httpClient = EVHttpClient.GetHttpClient(); try { HttpResponseMessage httpResp; if (null == request) { httpResp = httpClient.Send(httpMethod, uri); } else { httpResp = httpClient.Send(httpMethod, uri, HttpContentExtensions.CreateXmlSerializable <TRequest>(request)); } if (!httpResp.IsStatusIsSuccessful()) { throw new EVException().AddHttpResponse(httpResp); } // -- get the xml return(httpResp.Content.ReadAsStream()); } catch (Exception exception) { CheckAndThrowWebException(exception); throw; } }
/// <summary> /// Puts the specified URI. /// </summary> /// <typeparam name="TRequest">The type of the request.</typeparam> /// <param name="uri">The URI.</param> /// <param name="obj">The obj.</param> public static void Put <TRequest>(string uri, TRequest obj) { HttpResponseMessage httpResp = GetHttpClient().Put(uri, HttpContentExtensions.CreateXmlSerializable <TRequest>(obj)); if (!httpResp.IsStatusIsSuccessful()) { throw new EVException().AddHttpResponse(httpResp); } }
/// <summary> /// Authenticates the by GUID. /// </summary> /// <param name="uri">The URI.</param> /// <param name="securityToken">The security token.</param> private static HttpClient AuthenticateByGuid(string uri, string securityToken) { const string SESSION_ID = "ASP.Net_SessionId"; const string UserGuid = "UserGuid"; //Stores the sessionid generated and returned by the server string sessionId = string.Empty; HttpResponseMessage httpResp = null; HeaderValues <Cookie> myCookies; HttpClient httpClient = new HttpClient(); httpClient.DefaultHeaders.Add(UserGuid, securityToken); httpResp = httpClient.Post(uri, HttpContentExtensions.CreateXmlSerializable(string.Empty)); if (!httpResp.IsStatusIsSuccessful()) { throw new EVException().AddHttpResponse(httpResp); } myCookies = httpResp.Headers.SetCookie; if (myCookies != null) { foreach (Cookie var in myCookies.Where(var => var.ContainsKey(SESSION_ID))) { // -- set the session ID sessionId = var[SESSION_ID]; break; } } Cookie objCookie = new Cookie(); objCookie.Add(Constants.EvolutionCookieName, sessionId); // -- set the cookie in the header httpClient.DefaultHeaders.Cookie.Add(objCookie); //Add authorization header Credential evcredential = new Credential(); evcredential.Parameters.Add(sessionId); httpClient.DefaultHeaders.Authorization = evcredential; httpClient.TransportSettings.ConnectionTimeout = TimeSpan.FromMinutes(60); httpClient.TransportSettings.ReadWriteTimeout = TimeSpan.FromMinutes(60); return(httpClient); }
/// <summary> /// This is a generic Http send method which takes data from /// stub if needed else calls the service and returns the data /// back to the caller /// </summary> /// <typeparam name="TRequest">Request type</typeparam> /// <typeparam name="TResponse">Response type</typeparam> /// <param name="httpMethod">HttpMethod type</param> /// <param name="uri">Uri of stubbed filename/Rest web service</param> /// <param name="obj">Request type object</param> /// <returns>Response type object</returns> public TResponse Send <TRequest, TResponse>(HttpMethod httpMethod, string uri, TRequest obj) where TRequest : class { HttpResponseMessage httpResp = null; string xml = string.Empty; // -- try to get stub data xml = GetStubData(uri); if (string.IsNullOrEmpty(xml)) { httpResp = obj == null?EVHttpClient.GetHttpClient().Send(httpMethod, uri) : EVHttpClient.GetHttpClient().Send(httpMethod, uri, HttpContentExtensions.CreateXmlSerializable <TRequest>(obj)); if (!httpResp.IsStatusIsSuccessful()) { throw new EVException().AddHttpResponse(httpResp); } // -- get response data xml = GetResponseData(uri, httpResp); } return((TResponse)XmlUtility.DeserializeObject(xml, typeof(TResponse))); }
/// <summary> /// A Get method to fetch the stream from the URI /// </summary> /// <typeparam name="TRequest">The Request Object to pass to</typeparam> /// <param name="httpMethod">HTTP Method</param> /// <param name="uri">The Source URL from where Stream can be downloaded</param> /// <param name="obj">The input object which should be passed as part of request</param> /// <returns></returns> public Stream GetStream <TRequest>(HttpMethod httpMethod, string uri, TRequest obj) where TRequest : class { HttpResponseMessage httpResp = null; string xml = string.Empty; // -- try to get stub data xml = GetStubData(uri); if (string.IsNullOrEmpty(xml)) { httpResp = obj == null?EVHttpClient.GetHttpClient().Send(httpMethod, uri) : EVHttpClient.GetHttpClient().Send(httpMethod, uri, HttpContentExtensions.CreateXmlSerializable <TRequest>(obj)); if (!httpResp.IsStatusIsSuccessful()) { throw new EVException().AddHttpResponse(httpResp); } // -- get response data return(httpResp.Content.ReadAsStream()); } return(null); }
private RestContent SendHttpRequest <T>( string url, RestRequest request, T objectToSend) where T : class { if (log.IsDebugEnabled) { log.DebugFormat( "Calling OE REST (operation='{0}', url='{1}')", request.Operation, url); } Uri requestUrl = new Uri(url); HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url); webRequest.Method = request.Operation.ToString(); webRequest.ContentType = "text/xml"; SetupWebRequest(webRequest); try { if (objectToSend != null) { using (Stream requestStream = webRequest.GetRequestStream()) { using (HttpContent httpContent = HttpContentExtensions.CreateXmlSerializable(objectToSend)) { if (log.IsDebugEnabled) { httpContent.LoadIntoBuffer(); log.DebugFormat("Sending XML: {0}", httpContent.ReadAsString()); } httpContent.WriteTo(requestStream); } } } using (HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse()) { RaiseOERestExceptionIfNeeded(requestUrl, response); RestContent oecontent; oecontent = new RestContent( ReadResponseAsString(response), requestUrl); if (log.IsDebugEnabled) { log.DebugFormat("Response: '{0}'", oecontent.ContentString); } return(oecontent); } } catch (WebException ex) { if (ex.Response == null) { if (log.IsDebugEnabled) { log.Debug("ex.Response == null"); } string message = string.Format( CultureInfo.InvariantCulture, "The call to '{0}' failed", url); throw new RestException(requestUrl, message, ex); } RaiseOERestExceptionIfNeeded(requestUrl, (HttpWebResponse)ex.Response); throw; } catch (Exception ex) { string message = string.Format( CultureInfo.InvariantCulture, "Exception while calling '{0}'", url); log.Warn(message, ex); throw; } }