Call a webpage from c# in code Call a webpage from c# in code asp.net asp.net

Call a webpage from c# in code


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.


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!!}


For not having you application to hang you will need to call the method from a Thread.

For the HTTP request without an answer, something like that should do the job:

Thread myThread = new Thread(new ThreadStart(myMethodThatDoHttp));    myThread.Start();public void myMethodThatDoHttp(){    HttpWebRequest request  = (HttpWebRequest)WebRequest.Create("http://www..com");    HttpWebResponse response = (HttpWebResponse)request.GetResponse();}