Following example I am trying to explain how we can request to a URL from our coding and read the Header content of the response.
Create a web application and add a Textbox and a Button to Default.aspx and double click on the button and add following code.
protected void Button1_Click(object sender, EventArgs e)
{
try
{
//Create a object for HttpWebRequest using this request object we will
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(TextBox1.Text);
//request a response from the URL
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
//Reading all header content of response
for (int i = 0; i < res.Headers.Count; i++)
{
Response.Write(res.Headers.AllKeys[i] + " : " + res.Headers[i].ToString() + "
");
}
}
catch (Exception ex)
{
//Catch if there are any exception
Response.Write(ex.Message);
}
}
Now we are ready with code, lets build, run and test
Hope this helps.
SatishKumar J
Microsoft MVP(ASP.NET)
Tags : HttpWebRequest, HttpWebResponse, Headers
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment