Advertisement
cgrunwald

Untitled

Sep 5th, 2010
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.50 KB | None | 0 0
  1. using System;
  2. using System.Net;
  3. using System.Text;
  4.  
  5. namespace FiddlerRequestToCodePlus
  6. {
  7.     public static class ClonedWebRequest
  8.     {
  9.     //Calls request functions sequentially.
  10.         static ClonedWebRequest()
  11.         {
  12.             HttpWebResponse response;
  13.             if (Request_www_google_com(out response))
  14.             {
  15.                 //Success, possibly use response.
  16.                 response.Close();
  17.             }
  18.             else
  19.             {
  20.                 //Failure, cannot use response.
  21.             }
  22.         }
  23.  
  24.  
  25.         /// <summary>
  26.         /// Tries to request the URL: http://www.google.com/
  27.         /// </summary>
  28.         /// <param name="response">After the function has finished, will possibly contain the response to the request.</param>
  29.         /// <returns>True if the request was successful; false otherwise.</returns>
  30.         static bool Request_www_google_com(out HttpWebResponse response)
  31.         {
  32.             response = null;
  33.  
  34.             try
  35.             {
  36.                 //Create request to URL.
  37.                 HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.google.com/");
  38.  
  39.                 //Set request headers.
  40.                 request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8";
  41.                 request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8,application/json";
  42.                 request.Headers.Set(HttpRequestHeader.AcceptLanguage, "en-us,en;q=0.5");
  43.                 request.Headers.Set(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
  44.                 request.Headers.Set(HttpRequestHeader.AcceptCharset, "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
  45.                 request.Headers.Add("Keep-Alive", "115");
  46.                 request.KeepAlive = true;
  47.                 request.Headers.Set(HttpRequestHeader.Cookie, @"PREF=ID=095864ae848ee5d3:U=338ba9bb0fe625b3:LD=en:NR=100:CR=2:TM=1283685796:LM=1283685796:GM=1:IG=3:S=D3-ZdATPqAvDPjcz; NID=38=1GeLw2zOceGNIzkneGS8biUaVyUiqzQM8tC-407Wy3GmGH_p_IaLgyxV0Rw0htvD8r-Qs6CHJ3LtrvlJjT-frFyZiiAJU_Zx5ZBdDW-nDQyvR78qX3ZXLI9woRqNPbUb; rememberme=true; SID=DQAAAJUAAACVkDK_0mGPIRbFONToK5IieAA_IfWamNG30p6_Q7FvO8h6HmiVu0Ns5O1isWvVmr7i7BUT7AURN2W_qwsLIPtoWgPrc1Ke4grNdIQ4t5ZWqDY804PT4L3RG2k8dG6L9mlko82aDTyhaIbLtpKEJIEAQ8J3TLm1qyiuJERFEjqSd8jyUC2yfTtSMmIjWY9QK2lQEKtsDEoUJn1zI5VERuHx; HSID=AaUcLGhxqvs_nq4eA; TZ=420");
  48.  
  49.                 //Get response to request.
  50.                 response = (HttpWebResponse)request.GetResponse();
  51.             }
  52.             catch (WebException e)
  53.             {
  54.                 //ProtocolError indicates a valid HTTP response, but with a non-200 status code (e.g. 304 Not Modified, 404 Not Found)
  55.                 if (e.Status == WebExceptionStatus.ProtocolError) response = (HttpWebResponse)e.Response;
  56.                 else return false;
  57.             }
  58.             catch (Exception)
  59.             {
  60.                 if(response != null) response.Close();
  61.                 return false;
  62.             }
  63.  
  64.             return true;
  65.         }
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement