Header of HttpWebRequest : HttpWebRequest « Network « ASP.Net






Header of 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 = "http://www.ebay.com";

  HttpWebRequest webRequestObject = null;
  StreamReader sr = null;
  HttpWebResponse webResponseObject = null;
  
  try{

    webRequestObject = (HttpWebRequest) WebRequest.Create(RequestUrl);
    webRequestObject.Method = "GET";
    
    webResponseObject = (HttpWebResponse) webRequestObject.GetResponse();
    
    int i;
    HeadersText.Text = "";
    for(i=0; i < webResponseObject.Headers.Count; i++){
      HeadersText.Text += Environment.NewLine + "Header Name: " + webResponseObject.Headers.Keys[i] + "   Value :" + webResponseObject.Headers[i];
    }
    
    CookieCollection myCookieCollection = webResponseObject.Cookies;
    for(i=0; i < myCookieCollection.Count; i++){
      HeadersText.Text += Environment.NewLine + "Cookie Name: " + myCookieCollection[i].Name + "   Value= " + myCookieCollection[i].Value;
    }

    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 Headers and Cookies" OnClick="SubmitButton_Click" /><br>
      <asp:TextBox Runat="server" id="HeadersText" Width="780" Height="300" TextMode="MultiLine" /><br>
      <asp:TextBox Runat="server" id="WebResponseText" Width="780" Height="300" TextMode="MultiLine" /><br>
      <asp:Label Runat="server" ID="WebResponseLabel" />
    </form>
  </body>
</html>

 








Related examples in the same category

1.Read Stream from HttpWebRequest
2.Search Amazon
3.Read Cookie value from HttpWebRequest
4.Set UserAgent for HttpWebRequest
5.HttpWebRequest AllowAutoRedirect