开发者

Getting 404 error page content with Http WebRequest?

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. :)


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);
            }
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜