Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a webpage from c# in code

I need a way of calling a web page from inside my .net appliction.

But i just want to send a request to the page and not worry about the response.

As there are times when the response can take a while so i dont want it to hang the appliction.

I have been trying in side the page_load event

WebClient webC = new WebClient();
Uri newUri = new Uri("http://localhost:49268/dosomething.aspx");
webC.UploadStringAsync(newUri, string.Empty);

Even though its set to Async, it still seams to hang as the page wont finish rendering until the threads have finsished

like image 684
TheAlbear Avatar asked Nov 19 '08 17:11

TheAlbear


2 Answers

This should work for you:

System.Net.WebClient client = new System.Net.WebClient();
client.DownloadDataAsync(new Uri("http://some.url.com/some/resource.html"));

The WebClient class has events for notifying the caller when the request is completed, but since you don't care there shouldn't be anything else to it.

like image 187
ckramer Avatar answered Nov 05 '22 11:11

ckramer


Doak, Was almost there, but each time I put any of the request in a sepreate thread the page still wouldn't render until all the thread had finished running.

The best way I found was adjusting Doak's method, and just sticking a timeout in there and swallowing the error.

I know its a hack but it does work :P

WebRequest wr = WebRequest.Create("http://localhost:49268/dostuff.aspx");
wr.Timeout = 3500;

try
{
    HttpWebResponse response = (HttpWebResponse)wr.GetResponse();
}
catch (Exception ex)
{
    //We know its going to fail but that dosent matter!!
}
like image 14
TheAlbear Avatar answered Nov 05 '22 09:11

TheAlbear