public Uri MaterializeHttpUrl(WebServiceSettings webServiceSettings, HttpRequest httpRequest) { var sb = new StringBuilder(); var rawUrl = webServiceSettings.BaseUrl.AbsoluteUri; if (string.IsNullOrWhiteSpace(httpRequest.Path) == false) { rawUrl += "/"; rawUrl += httpRequest.Path; } if (httpRequest.RoutesValues != null) { rawUrl = httpRequest.RoutesValues.AllKeys.Aggregate( rawUrl, (current, routesValueKey) => current.Replace("{" + routesValueKey + "}", httpRequest.RoutesValues[routesValueKey])); } sb.Append(rawUrl); if (httpRequest.QueryString == null) { return(new Uri(sb.ToString())); } for (var i = 0; i < httpRequest.QueryString.Count; i++) { var key = httpRequest.QueryString.AllKeys[i]; // http://stackoverflow.com/questions/575440/url-encoding-using-c-sharp sb.AppendFormat("{0}{1}={2}", (i == 0 ? "?" : "&"), key, WebUtility.UrlEncode(httpRequest.QueryString[key])); } return(new Uri(sb.ToString())); }
public Uri MaterializeHttpUrl(WebServiceSettings webServiceSettings, HttpRequest httpRequest) { var sb = new StringBuilder(); var rawUrl = webServiceSettings.BaseUrl.AbsoluteUri; if (string.IsNullOrWhiteSpace(httpRequest.Path) == false) { rawUrl += "/"; rawUrl += httpRequest.Path; } if (httpRequest.RoutesValues != null) { rawUrl = httpRequest.RoutesValues.AllKeys.Aggregate( rawUrl, (current, routesValueKey) => current.Replace("{" + routesValueKey + "}", httpRequest.RoutesValues[routesValueKey])); } sb.Append(rawUrl); if (httpRequest.QueryString == null) return new Uri(sb.ToString()); for (var i = 0; i < httpRequest.QueryString.Count; i++) { var key = httpRequest.QueryString.AllKeys[i]; // http://stackoverflow.com/questions/575440/url-encoding-using-c-sharp sb.AppendFormat("{0}{1}={2}", (i == 0 ? "?" : "&"), key, WebUtility.UrlEncode(httpRequest.QueryString[key])); } return new Uri(sb.ToString()); }
public bool TransmitHttpRequest( WebServiceSettings settings, HttpRequest httpRequest, List <string> messages, out StreamReader streamReader) { var url = _httpUrlMaterializer.MaterializeHttpUrl(settings, httpRequest); _logger.Debug("Transporting request to '{0}'...", url); Stream responseStream; try { var webRequest = (HttpWebRequest)WebRequest.Create(url); webRequest.Method = httpRequest.HttpMethod.ToString(); if (httpRequest.Headers != null && httpRequest.Headers.Count > 0) { foreach (var headerKey in httpRequest.Headers.AllKeys) { webRequest.Headers.Add(headerKey, httpRequest.Headers[headerKey]); } } // TODO: Fix this case // probably httpRequest needs client implementation //if (httpRequest.PostData != null) //{ // var stream = webRequest.GetRequestStream(); // foreach (var bytes in httpRequest.PostData) // { // stream.Write(bytes, 0, bytes.Length); // } //} var webResponse = (HttpWebResponse)webRequest.GetResponse(); responseStream = webResponse.GetResponseStream(); } catch (Exception ex) { messages.Add("Unable to retreive response data from the network. Exception:" + Environment.NewLine + ex.Message); streamReader = null; return(false); } if (responseStream == null) { messages.Add("Failed to obtain response from the connection."); streamReader = null; return(false); } streamReader = new StreamReader(responseStream); return(true); }
public bool TransmitHttpRequest( WebServiceSettings settings, HttpRequest httpRequest, List<string> messages, out StreamReader streamReader) { var url = _httpUrlMaterializer.MaterializeHttpUrl(settings, httpRequest); _logger.Debug("Transporting request to '{0}'...", url); Stream responseStream; try { var webRequest = (HttpWebRequest)WebRequest.Create(url); webRequest.Method = httpRequest.HttpMethod.ToString(); if (httpRequest.Headers != null && httpRequest.Headers.Count > 0) { foreach (var headerKey in httpRequest.Headers.AllKeys) { webRequest.Headers.Add(headerKey, httpRequest.Headers[headerKey]); } } // TODO: Fix this case // probably httpRequest needs client implementation //if (httpRequest.PostData != null) //{ // var stream = webRequest.GetRequestStream(); // foreach (var bytes in httpRequest.PostData) // { // stream.Write(bytes, 0, bytes.Length); // } //} var webResponse = (HttpWebResponse)webRequest.GetResponse(); responseStream = webResponse.GetResponseStream(); } catch (Exception ex) { messages.Add("Unable to retreive response data from the network. Exception:" + Environment.NewLine + ex.Message); streamReader = null; return false; } if (responseStream == null) { messages.Add("Failed to obtain response from the connection."); streamReader = null; return false; } streamReader = new StreamReader(responseStream); return true; }