Files
2025-10-11 09:44:08 -04:00

149 lines
4.1 KiB
C#

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Collections.Generic;
namespace Endpoint
{
/// <summary>
/// Http Endpoint - such as a webserver, a webservice, etc
/// </summary>
[Serializable]
public class HttpEndpoint : IServiceEndpoint
{
/// <summary>
/// Returns the response string grabbed from the passed in URL.
/// </summary>
/// <param name="url">The URL.</param>
/// <param name="contentType">The HTTP contentType.</param>
/// <param name="requestContent">The request content.</param>
/// <param name="webHeaders">The web headers.</param>
/// <returns>A string containing response</returns>
protected static string GetUrlContent(Uri url, string contentType, string requestContent, Dictionary<string,string> 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<string, string> 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;
/// <summary>
/// Returns a string explaining details on the current status
/// </summary>
/// <returns>string with status message</returns>
public virtual string StatusDescription
{
get; protected set;
}
public IServiceEndpointConfiguration ServiceEndpointConfiguration
{
get { return _httpEndpointConfiguration; }
}
/// <summary>
/// Gets the service name
/// </summary>
/// <value></value>
public string ServiceName
{
get { return _httpEndpointConfiguration.Name; }
}
/// <summary>
/// Gets the HTTP endpoint configuration.
/// </summary>
/// <value>The HTTP endpoint configuration.</value>
public IHttpEndpointConfiguration HttpEndpointConfiguration
{
get { return _httpEndpointConfiguration; }
}
/// <summary>
/// Gets the current status result of the endpoint
/// </summary>
/// <returns>Status</returns>
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;
}
}
/// <summary>
/// Initializes a new instance of the <see cref="HttpEndpoint"/> class.
/// </summary>
/// <param name="config">The HttpEndpointConfiguration.</param>
public HttpEndpoint(IHttpEndpointConfiguration config)
{
_httpEndpointConfiguration = config;
}
}
}