views:

76

answers:

1

What is the best and simplest http user agent in .NET?

I simply want to put in the url have it return the page as a string.

+2  A: 

Thanks to @ion todriel, a suggestion based on System.Net.HttpWebRequest:

using System;
using System.Collections.Generic;
using System.Net;
using System.IO;

namespace myHttpWebRequest
{
    class Program
    {
        static void Main(string[] args)
        {
            var request = HttpWebRequest.Create("http://www.example.com");
            var response = request.GetResponse();
            var reader = new StreamReader(response.GetResponseStream());
            string page = reader.ReadToEnd();
            Console.Write(page);
        }
    }
}

Note the line string page = reader.ReadToEnd (); - return the whole page as a string.

This is not more complicated than the earlier System.Net.WebClinet with an example in the reference document.

gimel
C-e C-b C-b C-t
Bertrand Marron
Simple, yet doomed. WebClient it's very poorly written, it inherits from Component, it needs to be disposed, and it's impractical in real-world scenarios where server response are sometimes beyond 1 second (WebClient does not set a timeout interval and the default one is quite small). Do yourself a favor an use WebRequest.
Ion Todirel
Thanks - @Ion Todriel. Modified to show HttpWebRquest.
gimel