Call a webpage from c# in code

2019-02-06 05:37发布

问题:

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

回答1:

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.



回答2:

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


回答3:

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();
}


回答4:

Look at System.Net.WebClient, specifically use the DownloadDataAsync() method to send the request without blocking the rest of the app.



回答5:

To have this process without interrupting the page flow of your current page I would recommend creating a WCF service that will execute the code for you. Have the service set to use 1 way calls and on the page initiate an Ajax call to the service.



回答6:

  1. As far as I can see the 'BeginGetResponse'-method and the 'EndGetResponse'-method of the HttpWebRequest-object (gained through the call of WebRequest.Create), resp. the BeginGetRequestStream-/EndGetRequestStream methods, HERE aren't reflected yet, - although they are EXLPLICITLY marked for an "async request" in the docs:

    • For .Net 2.0
    • For .Net 4.0

    No clue, how that works.

  2. If the page to call is on the same IIS/App. as the calling, you could write an Filter, that ends all service for this request to client (Your 'calling' aspx-page), if the map-string contains the spec for the page to call (in ISAPI-cpp: "return SF_STATUS_REQ_FINISHED;") But perhaps Your asp/x/-script-execution in the called page is killed too. Question of check.

  3. Consider using server-side include directives (less conditionable) or dynamically call an .asp file by using Server.Execute.

  4. Not really async, but maybe worthy: State explicitly by an EARLY Response.End() (or similar) IN THE CALLED PAGE to the system, that NO FURTHER RESPONSE is to be expected, mainly NOT to the caller. Then do Your stuff ongoing in the CALLED page's scripts. At least the time-slip to await the sync-call's ending could be minimized by fac of 10, 100 or so.



回答7:

Use System.Net.WebClient.DownloadDataAsync/DownloadFileAsync in conjunction with DownloadDataCompleted/DownloadFileCompleted.