1. //ORIGINALLY FOUND ON STACKOVERFLOW
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Net;
  8. using System.IO;
  9.  
  10. namespace myWeb
  11. {
  12.     public class RequestManager
  13.     {
  14.         public string LastResponse { protected set; get; }
  15.  
  16.         CookieContainer cookies = new CookieContainer();
  17.  
  18.         internal string GetCookieValue(Uri SiteUri, string name)
  19.         {
  20.             Cookie cookie = cookies.GetCookies(SiteUri)[name];
  21.             return (cookie == null) ? null : cookie.Value;
  22.         }
  23.  
  24.         public string GetResponseContent(HttpWebResponse response)
  25.         {
  26.             if (response == null)
  27.             {
  28.                 throw new ArgumentNullException("response");
  29.             }
  30.             Stream dataStream = null;
  31.             StreamReader reader = null;
  32.             string responseFromServer = null;
  33.  
  34.             try
  35.             {
  36.                 // Get the stream containing content returned by the server.
  37.                 dataStream = response.GetResponseStream();
  38.                 // Open the stream using a StreamReader for easy access.
  39.                 reader = new StreamReader(dataStream);
  40.                 // Read the content.
  41.                 responseFromServer = reader.ReadToEnd();
  42.                 // Cleanup the streams and the response.
  43.             }
  44.             catch (Exception ex)
  45.             {
  46.                 Console.WriteLine(ex.Message);
  47.             }
  48.             finally
  49.             {
  50.                 if (reader != null)
  51.                 {
  52.                     reader.Close();
  53.                 }
  54.                 if (dataStream != null)
  55.                 {
  56.                     dataStream.Close();
  57.                 }
  58.                 response.Close();
  59.             }
  60.             LastResponse = responseFromServer;
  61.             return responseFromServer;
  62.         }
  63.  
  64.         public HttpWebResponse SendPOSTRequest(string uri, string content, string login, string password, bool allowAutoRedirect)
  65.         {
  66.             HttpWebRequest request = GeneratePOSTRequest(uri, content, login, password, allowAutoRedirect);
  67.             return GetResponse(request);
  68.         }
  69.  
  70.         public HttpWebResponse SendGETRequest(string uri, string login, string password, bool allowAutoRedirect)
  71.         {
  72.             HttpWebRequest request = GenerateGETRequest(uri, login, password, allowAutoRedirect);
  73.             return GetResponse(request);
  74.         }
  75.  
  76.         public HttpWebResponse SendRequest(string uri, string content, string method, string login, string password, bool allowAutoRedirect)
  77.         {
  78.             HttpWebRequest request = GenerateRequest(uri, content, method, login, password, allowAutoRedirect);
  79.             return GetResponse(request);
  80.         }
  81.  
  82.         public HttpWebRequest GenerateGETRequest(string uri, string login, string password, bool allowAutoRedirect)
  83.         {
  84.             return GenerateRequest(uri, null, "GET", null, null, allowAutoRedirect);
  85.         }
  86.  
  87.         public HttpWebRequest GeneratePOSTRequest(string uri, string content, string login, string password, bool allowAutoRedirect)
  88.         {
  89.             return GenerateRequest(uri, content, "POST", null, null, allowAutoRedirect);
  90.         }
  91.  
  92.         internal HttpWebRequest GenerateRequest(string uri, string content, string method, string login, string password, bool allowAutoRedirect)
  93.         {
  94.             if (uri == null)
  95.             {
  96.                 throw new ArgumentNullException("uri");
  97.             }
  98.             // Create a request using a URL that can receive a post.
  99.             HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
  100.             // Set the Method property of the request to POST.
  101.             request.Method = method;
  102.             // Set cookie container to maintain cookies
  103.             request.CookieContainer = cookies;
  104.             request.AllowAutoRedirect = false;
  105.             // If login is empty use defaul credentials
  106.             if (string.IsNullOrEmpty(login))
  107.             {
  108.                 request.Credentials = CredentialCache.DefaultNetworkCredentials;
  109.             }
  110.             else
  111.             {
  112.                 request.Credentials = new NetworkCredential(login, password);
  113.             }
  114.             if (method == "POST")
  115.             {
  116.                 // Convert POST data to a byte array.
  117.                 byte[] byteArray = Encoding.UTF8.GetBytes(content);
  118.                 // Set the ContentType property of the WebRequest.
  119.                 request.ContentType = "application/x-www-form-urlencoded";
  120.                 // Set the ContentLength property of the WebRequest.
  121.                 request.ContentLength = byteArray.Length;
  122.                 // Get the request stream.
  123.                 Stream dataStream = request.GetRequestStream();
  124.                 // Write the data to the request stream.
  125.                 dataStream.Write(byteArray, 0, byteArray.Length);
  126.                 // Close the Stream object.
  127.                 dataStream.Close();
  128.             }
  129.             return request;
  130.         }
  131.  
  132.         internal HttpWebResponse GetResponse(HttpWebRequest request)
  133.         {
  134.             if (request == null)
  135.             {
  136.                 throw new ArgumentNullException("request");
  137.             }
  138.             HttpWebResponse response = null;
  139.             try
  140.             {
  141.                 response = (HttpWebResponse)request.GetResponse();
  142.                 cookies.Add(response.Cookies);
  143.                 // Print the properties of each cookie.
  144.                 Console.WriteLine("\nCookies: ");
  145.                 foreach (Cookie cook in cookies.GetCookies(request.RequestUri))
  146.                 {
  147.                     Console.WriteLine("Domain: {0}, String: {1}", cook.Domain, cook.ToString());
  148.                 }
  149.             }
  150.             catch (WebException ex)
  151.             {
  152.                 Console.WriteLine("Web exception occurred. Status code: {0}", ex.Status);
  153.             }
  154.             catch (Exception ex)
  155.             {
  156.                 Console.WriteLine(ex.Message);
  157.             }
  158.             return response;
  159.         }
  160.  
  161.     }
  162. }