在代码中从c#调用网页

时间:2021-02-14 15:06:22

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

我需要一种从我的.net应用程序内部调用网页的方法。

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

我一直在尝试使用page_load事件

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

即使它设置为Async,它仍然会挂起,因为页面不会完成呈现,直到线程已经完成

7 个解决方案

#1


13  

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.

WebClient类具有用于在请求完成时通知调用者的事件,但由于您不关心,因此不应该有任何其他内容。

#2


12  

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.

Doak,几乎就在那里,但每次我将任何请求放在一个sepreate线程中时,页面仍然无法渲染,直到所有线程都完成运行。

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

我找到的最好的方法是调整Doak的方法,只是在那里停留超时并吞下错误。

I know its a hack but it does work :P

我知道它是一个黑客但它确实有效: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


9  

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

如果没有挂起应用程序,则需要从Thread调用该方法。

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

对于没有答案的HTTP请求,类似的东西应该做的工作:

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


7  

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

查看System.Net.WebClient,特别是使用DownloadDataAsync()方法发送请求,而不会阻止应用程序的其余部分。

#5


3  

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.

要在不中断当前页面页面流的情况下执行此过程,我建议您创建一个将为您执行代码的WCF服务。将服务设置为使用单向调用,并在页面上启动对服务的Ajax调用。

#6


2  

  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:

    至于我可以看到'BeginGetResponse'方法和HttpWebRequest对象的'EndGetResponse'方法(通过调用WebRequest.Create获得),resp。 BeginGetRequestStream- / EndGetRequestStream方法,HERE尚未反映出来, - 尽管它们在文档中被明确标记为“异步请求”:

    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.

    如果要调用的页面位于同一IIS / App上。作为调用,您可以编写一个过滤器,如果map-string包含要调用的页面的规范(在ISAPI-cpp中:“返回”,则会结束对客户端的此请求的所有服务(您的'调用'aspx-page) SF_STATUS_REQ_FINISHED;“)但也许您在被调用页面中的asp / x / -script执行也被杀死了。检查问题。

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

    考虑使用服务器端include指令(不太可调)或使用Server.Execute动态调用.asp文件。

  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.

    不是真的异步,但可能是值得的:明确地通过EARLY Response.End()(或类似的)在系统的呼叫页面中明确说明,没有进一步的响应是预期的,主要不是给呼叫者。然后在CALLED页面的脚本中继续进行。至少等待同步呼叫结束的时间间隔可以通过10,100左右的fac来最小化。

#7


2  

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

将System.Net.WebClient.DownloadDataAsync / DownloadFileAsync与DownloadDataCompleted / DownloadFileCompleted结合使用。

#1


13  

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.

WebClient类具有用于在请求完成时通知调用者的事件,但由于您不关心,因此不应该有任何其他内容。

#2


12  

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.

Doak,几乎就在那里,但每次我将任何请求放在一个sepreate线程中时,页面仍然无法渲染,直到所有线程都完成运行。

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

我找到的最好的方法是调整Doak的方法,只是在那里停留超时并吞下错误。

I know its a hack but it does work :P

我知道它是一个黑客但它确实有效: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


9  

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

如果没有挂起应用程序,则需要从Thread调用该方法。

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

对于没有答案的HTTP请求,类似的东西应该做的工作:

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


7  

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

查看System.Net.WebClient,特别是使用DownloadDataAsync()方法发送请求,而不会阻止应用程序的其余部分。

#5


3  

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.

要在不中断当前页面页面流的情况下执行此过程,我建议您创建一个将为您执行代码的WCF服务。将服务设置为使用单向调用,并在页面上启动对服务的Ajax调用。

#6


2  

  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:

    至于我可以看到'BeginGetResponse'方法和HttpWebRequest对象的'EndGetResponse'方法(通过调用WebRequest.Create获得),resp。 BeginGetRequestStream- / EndGetRequestStream方法,HERE尚未反映出来, - 尽管它们在文档中被明确标记为“异步请求”:

    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.

    如果要调用的页面位于同一IIS / App上。作为调用,您可以编写一个过滤器,如果map-string包含要调用的页面的规范(在ISAPI-cpp中:“返回”,则会结束对客户端的此请求的所有服务(您的'调用'aspx-page) SF_STATUS_REQ_FINISHED;“)但也许您在被调用页面中的asp / x / -script执行也被杀死了。检查问题。

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

    考虑使用服务器端include指令(不太可调)或使用Server.Execute动态调用.asp文件。

  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.

    不是真的异步,但可能是值得的:明确地通过EARLY Response.End()(或类似的)在系统的呼叫页面中明确说明,没有进一步的响应是预期的,主要不是给呼叫者。然后在CALLED页面的脚本中继续进行。至少等待同步呼叫结束的时间间隔可以通过10,100左右的fac来最小化。

#7


2  

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

将System.Net.WebClient.DownloadDataAsync / DownloadFileAsync与DownloadDataCompleted / DownloadFileCompleted结合使用。