My HttpWebRequestHelper class

using System;

using System.Text.RegularExpressions;

using System.IO;

using System.Diagnostics;

using System.Net;

namespace FSHelperLib

{

      using System;

      using System.Text;

      // clarification from

//    System.Web.HttpRequest is a class used on the server and inside an ASP.NET application. It represents the *incoming* request from a client.

//   System.Net.HttpWebRequest is a class used to make an *outgoing request to a web application.

      //The best ideas come from http://odetocode.com/Articles/162.aspx

      ///

      /// Summary description for HttpWebRequestHelper.

      ///

      public class HttpWebRequestHelper

      {

            // Methods

            public HttpWebRequestHelper

            {

            }

        //only cookies returned, actual response is ignored

        public static CookieContainer PostWithCookies(string sUrl, string postData, string saveHtml)

            {    

                  // have a cookie container ready to receive the forms auth cookie

                  CookieContainer cntnrCookies = new CookieContainer;

                  return PostWithCookies(sUrl, postData, saveHtml, ref cntnrCookies);

            }

            //only cookies returned, actual response is ignored

            public static CookieContainer PostWithCookies(string sUrl,string postData,string saveHtml,ref CookieContainer cntnrCookies)

            {                

                  if(cntnrCookies==null)

                  {

                     cntnrCookies = new CookieContainer; 

                  }

                  HttpWebResponse resp=PostGetHttpWebResponse(sUrl, postData,ref cntnrCookies);

                  DebugHelper.TracedLine(" resp.ResponseURI=" + resp.ResponseUri );

                  Stream strmResponse = resp.GetResponseStream;

                  if (!DataHelper.IsNullOrEmpty(saveHtml))

                              StreamHelper.SaveToFile(strmResponse,saveHtml);

                  DebugHelper.PrintCookies(new Uri(sUrl),cntnrCookies);

                  return      cntnrCookies;

            }

            public static HttpWebResponse PostGetHttpWebResponse(string sUrl,string postData,ref CookieContainer cntnrCookies)

            {

                  //from http://odetocode.com/Articles/162.aspx

            HttpWebRequest webRequest = PreparePostRequest(sUrl, postData, cntnrCookies);

                  HttpWebResponse resp=webRequest.GetResponse as HttpWebResponse;

            //from http://www.codeproject.com/csharp/clientticket\_msnp9.asp

            while((resp.StatusCode == HttpStatusCode.Redirect) )//add? || (resp.StatusCode == HttpStatusCode.Moved ) )

                  {// If the statuscode is 302, then there is a redirect, read the new adres en connect again

                string uri = resp.Headers.Get("Location");

                // Make a new request

                webRequest = (HttpWebRequest)HttpWebRequest.Create(uri); //get, not post

                webRequest.CookieContainer = cntnrCookies;

                //try to change extra options if required - from http://www.codeproject.com/csharp/ClientTicket_MSNP9.asp         

                resp = (HttpWebResponse)webRequest.GetResponse;

            }

                  DebugHelper.PrintHttpWebResponse(resp,TraceHelper.LineWithTrace(""));

                  DebugHelper.PrintCookies(webRequest.RequestUri,cntnrCookies);

                  //DebugHelper.PrintCookies(resp.Cookies,"Response.Cookies" );

                  return      resp;

            }

        private static HttpWebRequest PreparePostRequest(string sUrl, string postData, CookieContainer cntnrCookies)

        {

            //from http://odetocode.com/Articles/162.aspx

            // first, request the login form to get the viewstate value

            // now post to the login form

            HttpWebRequest webRequest = PrepareWebRequest(sUrl, "POST", cntnrCookies);

            // write the form values into the request message

            StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream);

            requestWriter.Write(postData);

            requestWriter.Close;

            return webRequest;

        }

        //The Method property can be set to any of the HTTP 1.1 protocol verbs: GET, HEAD, POST, PUT, DELETE, TRACE, or OPTIONS.

        public static HttpWebRequest PrepareWebRequest(string sUrl, string Method, CookieContainer cntnrCookies)

        {

            HttpWebRequest webRequest = WebRequest.Create(sUrl) as HttpWebRequest;

            webRequest.Method = Method;

            webRequest.ContentType = "application/x-www-form-urlencoded";

            webRequest.CookieContainer = cntnrCookies;

            /*                //try to change - from http://www.codeproject.com/csharp/ClientTicket\_MSNP9.asp         

                        webRequest.AllowAutoRedirect = false;

                       webRequest.Pipelined = false;

                        webRequest.KeepAlive = false;

                        webRequest.ProtocolVersion = new Version(1,0);//protocol 1.0 works better that 1.1??

            */

            //MNF 26/5/2005 Some web servers expect UserAgent to be specified

            //so let's say it's IE6

            webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0;.NET CLR 1.1.4322)";

            DebugHelper.PrintHttpWebRequest(webRequest, TraceHelper.LineWithTrace(""));

            return webRequest;

        }

        public static string PostGetResponseString(string sUrl, string postData, ref CookieContainer cntnrCookies, string saveHtml)

            {                

                  HttpWebResponse resp=PostGetHttpWebResponse(sUrl, postData,ref cntnrCookies);

            string sResp = GetResponseString(resp, saveHtml);

                  return sResp;

            }

 ///

       ///        ///        ///        /// verbs: GET, HEAD, POST, PUT, DELETE, TRACE, or OPTIONS.        /// can be null        /// specify null, if in production        ///        ///        /// HttpWebRequestHelper.GetResponseString ("", "GET", null, "temp.htm");

       ///

        public static string GetResponseString(string sUrl, string Method, CookieContainer cntnrCookies, string sFileToSaveHtml)

        {

            HttpWebRequest webRequest = HttpWebRequestHelper.PrepareWebRequest(sUrl, Method, cntnrCookies);

            HttpWebResponse resp = webRequest.GetResponse as HttpWebResponse;

            return HttpWebRequestHelper.GetResponseString(resp, sFileToSaveHtml);

        }

        public static string GetResponseString(HttpWebResponse resp, string saveHtml)

        {

            string sResp = StreamHelper.StreamToString(resp.GetResponseStream);

            resp.Close;

            if (!DataHelper.IsNullOrEmpty(saveHtml))

            {

                StreamHelper.SaveStringToFile(sResp, saveHtml);

            }

            return sResp;

        }

        public static bool CookieExists(string sUrl, CookieContainer container, string name)

            {

                  return CookieExists( new Uri( sUrl), container, name);

            }

            public static bool CookieExists( Uri uri,CookieContainer container, string name)

            {

                  if (container==null)

                  {

                        return false;

                  }

                  System.Net.CookieCollection cookies = container.GetCookies(uri);

                  return CookieExists(cookies,name );

            }

            public static bool CookieExists(CookieCollection cookies, string name)

            {

                  return (cookies[name]!=null);

            }

        public static string ExtractViewState(string sHtml)

        { //another implementation is in http://odetocode.com/Articles/162.aspx

            string sRet = "";

            try

            {

                String sPattern = @"name=""__VIEWSTATE"".value=""(.*?)""";// name="__VIEWSTATE".value="(.*?)"

                sRet = RegexMatchsHelper.CaptureFirstUnnamedGroup(sHtml, sPattern);

                //can return empty viewstate

                //if (String.IsNullOrEmpty(sRet))

                //{

                //    throw new FSCSharpLib.ExceptionWithDetails("The returned html has unexpected format", "html: " + sHtml);

                //}

            }

            catch (Exception exc)

            {

                throw new FSCSharpLib.ExceptionWithDetails("The returned html has unexpected format", "html: " + sHtml, exc);

            }

            //From http://odetocode.com/Articles/162.aspx:

            //Notice the use of URL encoding to make sure the server misinterprets no characters with a special meaning (like the equal sign).

            return System.Web.HttpUtility.UrlEncodeUnicode(sRet);

        }

        //Default method is Get, do not store cookies

        public static Stream GetResponseStream(string sUrl, IWebProxy proxy)

        {

            HttpWebRequest webRequest = WebRequest.Create(sUrl) as HttpWebRequest;

          // webRequest.ContentType = "application/x-www-form-urlencoded";//is good?

            webRequest.Proxy = proxy;

                  //you can change some properties if required - from http://www.codeproject.com/csharp/ClientTicket\_MSNP9.asp         

            //MNF 26/5/2005 Some web servers expect UserAgent to be specified,so let's say it's IE6

            webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0;.NET CLR 1.1.4322)";

            DebugHelper.PrintHttpWebRequest(webRequest, TraceHelper.LineWithTrace(""));

            HttpWebResponse resp = webRequest.GetResponse as HttpWebResponse;

            Stream strm   = resp.GetResponseStream;

            return strm;//'HttpWebRequestHelper.GetResponseString(resp, sFileToSaveHtml);

        }

      }// class HttpWebRequestHelper

}

This article is part of the GWB Archives. Original Author: Michael Freidgeim

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.