Read Cookie value from HttpWebRequest : HttpWebRequest « Network « ASP.Net

Home
ASP.Net
1.ADO.net Database
2.Ajax
3.Asp Control
4.Collections
5.Components
6.Data Binding
7.Development
8.File Directory
9.HTML Control
10.Language Basics
11.Login Security
12.Mobile Control
13.Network
14.Page
15.Request
16.Response
17.Server
18.Session Cookie
19.Sitemap
20.Theme Style
21.User Control and Master Page
22.Validation by Control
23.Validation by Function
24.WebPart
25.WPF
26.XML
ASP.Net » Network » HttpWebRequest 
Read Cookie value from HttpWebRequest


<%@ Page %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>
<script language="C#" runat="server">

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;
  
  try{

    webRequestObject = (HttpWebRequestWebRequest.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;
    CookieContainerObject.Add(Cookie);

    webRequestObject.CookieContainer = CookieContainerObject;

    webResponseObject = (HttpWebResponsewebRequestObject.GetResponse();
    sr = new StreamReader(webResponseObject.GetResponseStream());
    
    String Results = sr.ReadToEnd();
    WebResponseLabel.Text = Results;
    WebResponseText.Text = Results;
  }
  finally{
    try{
      sr.Close();
      webResponseObject.Close();
      webRequestObject.Abort();
    }
    catch{}
  }
}

</script>
<html>
  <head>
    <title></title>
  </head>
  <body>
    <form runat="server" ID="Form1">
      <asp:Button Runat="server" ID="SubmitButton" Text="Get Page Requiring Cookies" OnClick="SubmitButton_Click" />
      <a href="NextPage.aspx">View Page without Cookie</a><br>
      <asp:TextBox Runat="server" id="WebResponseText" Width="780" Height="300" TextMode="MultiLine" /><br>
      <asp:Label Runat="server" ID="WebResponseLabel" />
    </form>
  </body>
</html>

File: NextPage.aspx

<%@ Page EnableViewstate="False" %>
<%@ Import Namespace="System.Net" %>
<script language="VB" runat="server">

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
  Dim MyCookie as HttpCookie = Request.Cookies("userid")
  
  If MyCookie is nothing then
    Label.Text = "You need cookies turned on to view this page."
  Else
    Label.Text = "Thank you for turning on cookies. Your userid is: " & MyCookie.Value
  End If
End Sub

</script>
<html>
  <body>
    <form runat="server">
      <asp:Label Runat="server" ID="Label" />
    </form>
  </body>
</html>

 
Related examples in the same category
1.Read Stream from HttpWebRequest
2.Search Amazon
3.Set UserAgent for HttpWebRequest
4.HttpWebRequest AllowAutoRedirect
5.Header of HttpWebRequest
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.