2

I am writing a .dll that will execute Gets and Posts. In order to do so, i created this class:

public class BDCWebRequests
{
    // private attributes
    private static CookieContainer m_CookieJar;
    private static HttpWebRequest  m_HttpWebRequest;
    private static string          m_defaultUserAgent      = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.107 Safari/535.1";
    private static string          m_defaultContentType    = "application/x-www-form-urlencoded";

    // Public Properties
    public HttpWebRequest InternalWebRequest
    {
        get { return m_HttpWebRequest; }
    }

    /// <summary>
    /// Class Constructor
    /// </summary>
    public BDCWebRequests()
    {
        m_CookieJar = new CookieContainer();
    }

   // Methods Come Here...
}

What i am trying to achieve is a way for the user of this lib to configure the request properly using the "InternalWebRequest" property.

The usage would be something like this:

BDCWebRequests myInstance = new BDCWebRequests();
myInstance.InternalWebRequest.Referer = "refererUrl";
myInstance.AllowAutoRedirect          = true;
myInstance.Host                       = "hostUrl";

After doing so, there are Posts and Get Methods ( Here is the GET as an Example )

 public string Get(string url)
    {
        try
        {
            // Creating instance of HttpWebRequest based on URL received
            m_HttpWebRequest = (HttpWebRequest) WebRequest.Create (url);

            m_HttpWebRequest.Method                        = "GET";
            m_HttpWebRequest.UserAgent                     = m_defaultUserAgent;

            // Execute web request and wait for response
            using (HttpWebResponse resp = (HttpWebResponse) m_HttpWebRequest.GetResponse())
            {
              return new StreamReader(resp.GetResponseStream()).ReadToEnd();
            }

        }
        catch (Exception ex)
        {
            LogWriter.Error(ex);
        }

        return String.Empty;
    } 

Usage:

myInstance.Get("UrlForTheRequest");

Main Issue: I am having problem when a user Executes a GET or a POST,and after this, he tries to change any attribute of the internal instance of HttpWebRequest using the public property.

If a user Calls a GET for example,and after that he tries to :

myInstance.InternalWebRequest.Host = "", for example, it throws an error: "this property cannot be set after writing has started"

Any idea of how to Logically implement it so that a user can :

1 - Configure the request anywhere, anytime without getting me this error 2 - Execute Gets and Posts using the previously Configured Request ?

Sorry for that Long Question, thanks in advance for the patience. Please, do not TL:DR :)

Marcello Grechi Lins
  • 3,350
  • 8
  • 38
  • 72

1 Answers1

1

Simple: the moment you've sent the request, read all the required data from the response, and then create a new request, and copy all relevant parameters from old request to it.

Svarog
  • 2,188
  • 15
  • 21
  • You mean,at the end of a GET or POST method ? I create a new request with the same URL and overwrite the old one stored as an attribute ? Also, is there any way i can iterate over all Attributes of my HttpWebRequest ? Because i dont want to "if (request.attrName != null) then copy to new one" – Marcello Grechi Lins Jan 09 '12 at 12:52
  • You can use reflection to iterate over all Atttributes. More specifically: use the request.GetType().GetProperties() method. – Svarog Jan 09 '12 at 13:02
  • Also, most WebRequest properties are simply HTTP headers, so if you copy the Headers property, you've already done most of the work. – Svarog Jan 09 '12 at 13:03
  • OK. I will try it, if it works properly i will pick your answer,thanks again : – Marcello Grechi Lins Jan 09 '12 at 13:05
  • Im having problems figuring out how to add all the cloned properties from the old object, to the new one. I already "backed up" all the properties from the first object,but now i have to copy them over the new fresh one. Any Help ? – Marcello Grechi Lins Jan 09 '12 at 13:37
  • 1
    Check this question: http://stackoverflow.com/questions/619767/net-reflection-set-object-property. The second answer shows how to set a property using reflection – Svarog Jan 09 '12 at 13:53