3

I use Http WebRequest with Vb.Net to download content. Everything is working fine, but now I have a problem:

I want to download this website which is an example for a 404 Error page with content: http://www.boris-koch.de/404seiteyeah

But then I get this Error: "Webexception - 404". And I can't read the content of the page because the response is nothing. So do you know a way how to handle it and to get the content of the 404 error page? Thanks a lot. :)

1 Answer 1

9

You can access the WebResponse within the WebException via the Response property. That will have the response data in. For example, in C# (the VB code would be very similar):

using System;
using System.IO;
using System.Net;

class Program
{
    static void Main(string[] args)
    {
        string url = "http://www.boris-koch.de/404seiteyeah";
        WebRequest req = WebRequest.Create(url);
        try
        {
            using (WebResponse response = req.GetResponse())
            {
                Console.WriteLine("Didn't expect to get here!");
            }
        }
        catch (WebException e)
        {
            WebResponse response = e.Response;
            using (StreamReader reader =
                        new StreamReader(response.GetResponseStream()))
            {
                string text = reader.ReadToEnd();
                Console.WriteLine(text);
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.