Get response from website using webclient

We can get the response from the website using the
class WebClient. The following code will explain it clearly

The following code is the initial stage of working with the web access

WebClient MyWebClient = new WebClient();
MyWebClient.BaseAddress = "";
MyWebClient.DownloadFile("", "index.htm");
StreamReader MyStreamReader =new StreamReader(MyWebClient.OpenRead("index.htm"));

Console.WriteLine (MyWebClient.Headers.Count);
WebHeaderCollection Myheader = MyWebClient.ResponseHeaders;

for (int i = 0; i < Myheader.Count; i++)
Console.WriteLine(Myheader.GetKey(i) + " : " + header[i]);

Code Explanation

1. Create the instance of the Webclient.
2. Create the stream by getting the response from the website using webclient.
3. Read the stream using stramreader and use it.
4. We can collect the headers using the WebHeaderCollection.
5. using for loop we can get all the headers.



No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: