C# .NET - How to maintain the state of the httpwebrequest

Asked By abinav shankar on 14-Nov-11 09:40 AM
i have used the following code to get authenticated and i successfully achieved it now using this authentication i have to futher proceed to get response from the url"http://localhost:8080/geonetwork/srv/en/xml.metadata.get"
this is the code i have used to get authentication
protected void Page_Load(object sender, EventArgs e) 
        string RequestXML = "<request>" + 
                            "<username>admin</username>" + 
        string ServerURL = "http://localhost:8080/geonetwork/srv/en/xml.user.login"; 
        string ResponseXML = postRequest(RequestXML, ServerURL); 
        Label1.Text = ResponseXML; 
    private string postRequest(String RequestXML, string ServerURL) 
        int timeout = 90000; 
        int connectionLimit = 10; 
        string responseXML = string.Empty; 
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(ServerURL); 
            webRequest.Timeout = timeout; 
            webRequest.KeepAlive = true; 
            webRequest.ServicePoint.ConnectionLimit = connectionLimit; 
            webRequest.Method = "POST"; 
            webRequest.ContentType = "text/xml"; 
            byte[] byteArray = Encoding.UTF8.GetBytes(RequestXML); 
            Stream strm = webRequest.GetRequestStream(); 
            strm.Write(byteArray, 0, byteArray.Length); 
            HttpWebResponse webResponse =(HttpWebResponse)webRequest.GetResponse(); 
            Encoding enc = Encoding.GetEncoding("utf-8"); 
            StreamReader reader = new StreamReader(webResponse.GetResponseStream(),enc); 
            responseXML = reader.ReadToEnd(); 
        catch (Exception ex) 
            throw (ex); 
        return responseXML; 
using the above code i have to get the response from this url "http://localhost:8080/geonetwork/srv/en/xml.metadata.get" without losing the authentication pl help me in doing this
Anil Kumar replied to abinav shankar on 14-Nov-11 10:48 AM
Hi Abhinav,

You have to use the Network admin login credentials and let the service follow the requests over HTTPS. Your code is http stateless, define a state by doing the prementioned thing and let your server implement the same. It will make the connection possible between the client and the server.

Hope it helps you
Do update us again.
Thank you
Kirtan Patel replied to abinav shankar on 14-Nov-11 10:07 PM
you need to use cookie container to maintain state between subsequent request to WebServer .
Once you retrieve the cookies after authentication you can serialize that object to file and when yo need to resume back you can retrieve that object from file and Assign it to new HttpWebRequest
private void SubmitButton_Click(System.Object sender, System.EventArgs e)
  String RequestUrl = Request.Url.GetLeftPart(System.UriPartial.Authority) + Request.ApplicationPath + "/NextPage.aspx";
  HttpWebRequest webRequestObject = null;
  StreamReader sr = null;
  HttpWebResponse webResponseObject = null;
    webRequestObject = (HttpWebRequest)WebRequest.Create(RequestUrl);
    webRequestObject.Method = "GET";
    System.Net.CookieContainer CookieContainerObject = new System.Net.CookieContainer();
    System.Net.Cookie Cookie = new System.Net.Cookie();
    Cookie.Name = "userid";
    Cookie.Value = "1234567890";
    Cookie.Domain = Request.ServerVariables["HTTP_HOST"];
    Cookie.Secure = true;
    webRequestObject.CookieContainer = CookieContainerObject;
    webResponseObject = (HttpWebResponse)webRequestObject.GetResponse();
    sr = new StreamReader(webResponseObject.GetResponseStream());
    String Results = sr.ReadToEnd();
    WebResponseLabel.Text = Results;
    WebResponseText.Text = Results;
    catch { }