Access URL without receiving the reply

Asked

Viewed 327 times

2

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

In certain situations I would like to trigger page processing 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 don’t use the answer - for example, calling the asynchronous version of the method WebClient.DownloadString, at some point will enter the console application a string with the representation of all the HTML that my page normally mounts.

There is some way, in . NET, to make an HTTP request (preferably but not necessarily GET) and inform the server that I do not want the answer?

  • I don’t know if I understand what you want. Do you want to make the request but don’t want any response? Is that right? What’s the point? Know if the server is alive? You just don’t want to waste time on getting the answer?

  • @Mustache I just want to fire the process, and the answer can be very big. I’ll make the same request at once for several pages and their combined response can be several megas. Since the answer is irrelevant to the console application, I would like to not have to receive all this information.

  • Can it be with async? You already know the solution, right? :)

  • @bigown even though I use asynchronous methods the server response is still sent to my machine. That’s what I want to avoid.

  • As far as I know, there is no way to avoid any response, perhaps I can avoid the content coming. And I still can’t imagine why I need this.

  • I even know a way to avoid the answer to my application - manipulate the headers of TCP packets to let them know that the response address is different. But I can’t do that. And it wouldn’t be very nice if the address I put in existed, the recipient could look at it as a Ddos...

Show 1 more comment

1 answer

4


Maybe I can’t find the WebClient but seems to find WebRequest, just ask for the HEAD not the data. You cannot use the GET, this returns all content. I believe it is the best that can be done. For this you need set the requisition method class WebRequest. Have examples in the documentation without using the HEAD but just do this:

webRequest.Method = "HEAD";

I found a question in the OS who 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 I can help someone. But this part does not answer what was asked.

Essentially it’s catch up with the asynchronous version of the method used, so the application "stuck" is not waiting for the answer (one of the ways with Webrequest). You will still receive it, you may or may not discard it, but you need to receive it somehow.

using System.Net;

/* .. SNIP .. */

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

I put in the Github for future reference.

I liked the question because I ended up learning some things I didn’t know, although I knew the essence. I discovered that the term used for this is fire and Forget. I found some examples as this that work differently (basically came out of this response in the OS).

  • If we ever meet on the street remind me to buy you a beer for this answer.

  • Several days later... I think to use TRACE instead HEAD can be even more economical if the site meets this type of request :) Still, your response saved my life, so thanks again!

  • @Renan I do not know if it changes a lot and I do not know if it is worth the effort but depending on what it is to do can be until the most correct.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.