125 lines
3.4 KiB
C#
125 lines
3.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Net;
|
|
using System.Text;
|
|
|
|
namespace Endpoint
|
|
{
|
|
/// <summary>
|
|
/// Http Endpoint - such as a webserver, a webservice, etc
|
|
/// </summary>
|
|
[Serializable]
|
|
public class HttpEndpoint : IEndpoint
|
|
{
|
|
/// <summary>
|
|
/// Gets or sets the name.
|
|
/// </summary>
|
|
/// <value>The name.</value>
|
|
public string Name { get; set; }
|
|
|
|
/// <summary>
|
|
/// URI of the service
|
|
/// </summary>
|
|
public Uri Uri { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the URI string.
|
|
/// </summary>
|
|
/// <value>The URI string.</value>
|
|
public string UriString
|
|
{
|
|
get { return Uri.ToString(); }
|
|
set { Uri = new Uri(value); }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the response string grabbed from the passed in URL.
|
|
/// </summary>
|
|
/// <param name="url">The URL.</param>
|
|
/// <param name="contentType">The HTTP content type.</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))
|
|
{
|
|
var webRequest = (HttpWebRequest) WebRequest.Create(url);
|
|
|
|
// NOTE: need a proxy? Here's where it would go
|
|
// webRequest.Proxy = new WebProxy();
|
|
|
|
// SOAP
|
|
webRequest.ContentType = contentType;
|
|
if (webHeaders != null && webHeaders.Count > 0)
|
|
{
|
|
foreach (var webHeader in webHeaders)
|
|
webRequest.Headers.Add(webHeader.Key, webHeader.Value);
|
|
}
|
|
|
|
webRequest.Method = "POST";
|
|
|
|
//We need to count how many bytes we're sending. Post'ed Faked Forms should be name=value&
|
|
var bytes = Encoding.ASCII.GetBytes(requestContent);
|
|
webRequest.ContentLength = bytes.Length;
|
|
using (var requestStream = webRequest.GetRequestStream())
|
|
{
|
|
requestStream.Write(bytes, 0, bytes.Length); //write it to the stream
|
|
}
|
|
var 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 (var data = client.OpenRead(url))
|
|
using (var reader = new StreamReader(data))
|
|
{
|
|
return reader.ReadToEnd();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a string explaining details on the current status
|
|
/// </summary>
|
|
/// <returns>string with status message</returns>
|
|
public virtual string StatusDescription { get; protected set; }
|
|
|
|
/// <summary>
|
|
/// Gets the current status result of the endpoint
|
|
/// </summary>
|
|
/// <returns>Status</returns>
|
|
public virtual Status GetStatus()
|
|
{
|
|
try
|
|
{
|
|
var request = WebRequest.Create(Uri);
|
|
|
|
using (var response = (HttpWebResponse) request.GetResponse())
|
|
{
|
|
StatusDescription = response.StatusDescription;
|
|
return response.StatusCode == HttpStatusCode.OK
|
|
? Status.Up
|
|
: Status.Unreachable;
|
|
}
|
|
}
|
|
catch (WebException ex)
|
|
{
|
|
StatusDescription = ex.Message + " (" + ex.Status + ")";
|
|
|
|
return ex.Status == WebExceptionStatus.Timeout
|
|
? Status.Timeout
|
|
: Status.Unreachable;
|
|
}
|
|
}
|
|
}
|
|
}
|