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 :)