Access URL without receiving response

2

I have a page that performs several tasks. This page is open to system end users, who can access it via the browser.

In certain situations I would like to trigger page rendering with a console application. Something like:

using System.Net;

/* .. SNIP .. */

WebClient foo = new WebClient();
foo.DownloadString("http://siteDaAplicacao.com.br/Pagina.aspx");

My problem: all methods of the WebClient class, as far as I know, will bring the full HTTP response from the server. So even if I do not use the answer - for example, by calling the asynchronous version of the WebClient.DownloadString method, at some point a string will come into the console application with the representation of all the HTML that my page normally mounts.

Is there any way in .NET to make an HTTP request (preferably but not necessarily GET) and tell the server that I do not want the response?

    
asked by anonymous 05.11.2014 / 17:15

1 answer

3

Maybe it does not work with WebClient but seems to give WebRequest , just ask for HEAD and not data. You can not use GET , it returns all content. I think it's the best you can do. To do this you need setar o request method of the class WebRequest . You have examples in the documentation without using HEAD but you would only need to do this:

webRequest.Method = "HEAD";

I found a question in SO that did this:

class Program {
    static void Main(string[] args) {
        try {
            System.Net.WebRequest wc = System.Net.WebRequest.Create("http://www.imdb.com"); //args[0]);

            ((HttpWebRequest)wc).UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.19 (KHTML, like Gecko) Chrome/0.2.153.1 Safari/525.19";
            wc.Timeout = 1000;
            wc.Method = "HEAD";
            WebResponse res = wc.GetResponse();
            var streamReader = new System.IO.StreamReader(res.GetResponseStream());

            Console.WriteLine(streamReader.ReadToEnd());
        } catch (Exception ex) {
            Console.WriteLine(ex.Message);
        }
    }
}

I was responding when the AP showed that I was on the wrong track. I'll leave it here so you can help someone. But this part does not answer what was asked.

Essentially, pick up with the asynchronous version of method used , so the "stuck" application is not waiting for the response (

05.11.2014 / 17:56