EasyHTTP

EasyHTTP is a simple class which exposes the HTTP functionality of .NET as a number of simpler methods.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;

/// <summary>
/// Summary description for EasyHTTP
/// </summary>
public class EasyHTTP
{
    public enum HTTPMethod
    {
        HTTP_GET = 0,
        HTTP_POST = 1
    }

    public EasyHTTP()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public static string Send(string URL)
    {
        return Send(URL, "", HTTPMethod.HTTP_GET, "");
    }

    public static string Send(string URL, string PostData)
    {
        return Send(URL, PostData, HTTPMethod.HTTP_GET, "");
    }

    public static string Send(string URL, string PostData, HTTPMethod Method)
    {
        return Send(URL, PostData, Method, "");
    }

    public static string Send(string URL, string PostData, HTTPMethod Method, string ContentType)
    {
        HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(URL);
        HttpWebResponse Response;
        StreamWriter SW = null;
        StreamReader SR = null;
        String ResponseData;

        //Prepare Request Object
        Request.Method = Method.ToString().Substring(5);

        //Set form/post content-type if necessary
        if (Method == HTTPMethod.HTTP_POST && PostData != "" && ContentType == "")
        {
            ContentType = "application/x-www-form-urlencoded";
        }

        // Set Content-Type
        if (ContentType != "")
        {
            Request.ContentType = ContentType;
            Request.ContentLength = PostData.Length;
        }

        // Send Request, If Request
        if (Method == HTTPMethod.HTTP_POST)
        {
            try
            {
                SW = new StreamWriter(Request.GetRequestStream());
                SW.Write(PostData);
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
            finally
            {
                SW.Close();
            }
        }

        // Receive Response
        try
        {
            Response = (HttpWebResponse)Request.GetResponse();
            SR = new StreamReader(Response.GetResponseStream());
            ResponseData = SR.ReadToEnd();
        }
        catch (WebException Wex)
        {
            SR = new StreamReader(Wex.Response.GetResponseStream());
            ResponseData = SR.ReadToEnd();
            throw new Exception(ResponseData);
        }
        finally
        {
            SR.Close();
        }

        return ResponseData;
    }
}
Author Barrington Haynes

I have nothing more to say

Add Comment

Name
Comment
 

Your comment has been received and will be shown once it passes moderation.