You are not Logged in
Would you like to Login or Register

Today is: Friday, 21 November, 2008
Check this months hot topics

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;
    }
}
kick it on DotNetKicks.com del.icio.us digg Mister Wong YahooMyWeb Reddit Furl Spurl blogmarks
Barrington Haynes Skype
Author : Barrington Haynes
Published : Tuesday, 20 June, 2006

Barry is the coolest member of the Innovation Team - not only does he code he's also a rock star (lead vocals and guitarist in the bands BridgeFire and Best of Foo) - what a combination. Barry is extremely interested in the integration of software components and embraces a lot of the current hype around web 2.0 and related concepts as standard.

Add Comment

Enter your comment below and it will be submitted for moderation.

Your Name

Add Tag

Please enter tags for this article, seperated by semi-colon ;

View Tag's by : # articles | # views