title: Generic Response of a WebRequest
body: <p>I just wrote a C# Lib to handle WebRequests safely (Which can be found, with the code open , <a href="http://pastebin.com/2viG0cNL" rel="nofollow">Here</a>)</p>
  
  <p>At the moment, my GET Method will always return a string as the response, but sometimes like when fetching captchas from a site, it will need to return a Bitmap instead.</p>
  
  <p>How do i do This ? How do i use a sort of  Type to make this Get request as generic as possible, making possible for anyone to choose the type of response it will receive.</p>
  
  <p><strong>Improving Question:</strong></p>
  
  <p>This is what i tried now. It does not compiles because it says it can't convert a <code>String</code> to a Type <code>T</code> on this line : </p>
  
  <pre><code>response = (T) new StreamReader(resp.GetResponseStream()).ReadToEnd();
  </code></pre>
  
  <p>This is my new Method :</p>
  
  <pre><code>public T Get &lt;T&gt; (string url)
      {
          T response = default(T);
  
          // Checking for empty url
          if (String.IsNullOrEmpty(url))
          {
              throw new Exception("URL para o Request não foi configurada ou é nula.");
          }
  
          try
          {
              // Re-Creating Request Object to avoid exceptions
              m_HttpWebRequest = WebRequest.Create (url) as HttpWebRequest;
  
              m_HttpWebRequest.CookieContainer              = m_CookieJar;
              m_HttpWebRequest.Method                       = "GET";
              m_HttpWebRequest.UserAgent                    = m_userAgent;
              m_HttpWebRequest.ServicePoint.ConnectionLimit = m_connectionsLimit;
              m_HttpWebRequest.Timeout                      = m_timeout;
              m_HttpWebRequest.ContentType                  = m_contentType;
              m_HttpWebRequest.Referer                      = m_referer;
              m_HttpWebRequest.AllowAutoRedirect            = m_allowAutoRedirect;
  
              if (!String.IsNullOrEmpty(m_host))
              {
                  m_HttpWebRequest.Host = m_host;
              }
  
              // Execute web request and wait for response
              using (HttpWebResponse resp = (HttpWebResponse) m_HttpWebRequest.GetResponse())
              {
                response = (T) new StreamReader(resp.GetResponseStream()).ReadToEnd();
              }
          }
          catch (Exception ex)
          {
              m_error  = ex.ToString();
  
              if (m_logOnError)
                  LogWriter.Error(ex);
          }
  
          return response;
      } 
  </code></pre>