using System; using System.IO; using System.Net; using System.Text; using System.Collections.Generic; namespace Endpoint { /// /// Http Endpoint - such as a webserver, a webservice, etc /// [Serializable] public class HttpEndpoint : IServiceEndpoint { /// /// Returns the response string grabbed from the passed in URL. /// /// The URL. /// The HTTP contentType. /// The request content. /// The web headers. /// A string containing response protected static string GetUrlContent(Uri url, string contentType, string requestContent, Dictionary webHeaders) { // POST request if (!string.IsNullOrEmpty(contentType) && !string.IsNullOrEmpty(requestContent)) { HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url); // NOTE: proxy? // webRequest.Proxy = new WebProxy(); // SOAP webRequest.ContentType = contentType; if (webHeaders != null && webHeaders.Count > 0) foreach (KeyValuePair webHeader in webHeaders) webRequest.Headers.Add(webHeader.Key, webHeader.Value); // NOTE: SOAPACtion //webRequest.Headers.Add("SOAPAction", "\"urn:getPaymentKey\""); // FORM //webRequest.ContentType = "application/x-www-form-urlencoded"; webRequest.Method = "POST"; // webRequest.Accept = "text/xml"; //We need to count how many bytes we're sending. Post'ed Faked Forms should be name=value& byte[] bytes = Encoding.ASCII.GetBytes(requestContent); webRequest.ContentLength = bytes.Length; using (Stream requestStream = webRequest.GetRequestStream()) { requestStream.Write(bytes, 0, bytes.Length); //write it to the stream } WebResponse webResponse = webRequest.GetResponse(); if (webResponse == null) return null; using (var streamReader = new StreamReader(webResponse.GetResponseStream())) { return streamReader.ReadToEnd(); } } // GET Request var client = new WebClient(); using (Stream data = client.OpenRead(url)) using (var reader = new StreamReader(data)) { return reader.ReadToEnd(); } } protected readonly IHttpEndpointConfiguration _httpEndpointConfiguration; /// /// Returns a string explaining details on the current status /// /// string with status message public virtual string StatusDescription { get; protected set; } public IServiceEndpointConfiguration ServiceEndpointConfiguration { get { return _httpEndpointConfiguration; } } /// /// Gets the service name /// /// public string ServiceName { get { return _httpEndpointConfiguration.Name; } } /// /// Gets the HTTP endpoint configuration. /// /// The HTTP endpoint configuration. public IHttpEndpointConfiguration HttpEndpointConfiguration { get { return _httpEndpointConfiguration; } } /// /// Gets the current status result of the endpoint /// /// Status public virtual Status GetStatus() { try { WebRequest request = WebRequest.Create(HttpEndpointConfiguration.Uri); using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { StatusDescription = response.StatusDescription; if (response.StatusCode == HttpStatusCode.OK) return Status.Up; return Status.Unreachable; } } catch (WebException ex) { StatusDescription = ex.Message + " (" + ex.Status + ")"; // TODO: LOG if (ex.Status == WebExceptionStatus.Timeout) { return Status.Timeout; } return Status.Unreachable; } } /// /// Initializes a new instance of the class. /// /// The HttpEndpointConfiguration. public HttpEndpoint(IHttpEndpointConfiguration config) { _httpEndpointConfiguration = config; } } }