在代码中从 C# 调用网页
我需要一种从 .net 应用程序内部调用网页的方法。
但我只想向页面发送请求而不担心响应。
由于有时响应可能需要一段时间,所以我不希望它挂起应用程序。
我一直在 page_load 事件中尝试
WebClient webC = new WebClient();
Uri newUri = new Uri("http://localhost:49268/dosomething.aspx");
webC.UploadStringAsync(newUri, string.Empty);
即使它设置为异步,它仍然会挂起,因为页面在线程完成之前不会完成渲染
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这应该对您有用:
WebClient 类具有用于在请求完成时通知调用者的事件,但由于您不关心,所以不应该有其他任何事情。
This should work for you:
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,就快到了,但是每次我将任何请求放入单独的线程中时,页面仍然不会呈现,直到所有线程完成运行。
我发现最好的方法是调整 Doak 的方法,并在那里设置一个超时并吞掉错误。
我知道这是一个 hack,但它确实有效:P
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
为了不让您的应用程序挂起,您需要从线程调用该方法。
对于没有答案的 HTTP 请求,类似的事情应该可以完成工作:
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:
查看
System.Net.WebClient
< /a>,具体使用DownloadDataAsync()
方法发送请求而不阻塞应用程序的其余部分。Look at
System.Net.WebClient
, specifically use theDownloadDataAsync()
method to send the request without blocking the rest of the app.为了在不中断当前页面的页面流的情况下完成此过程,我建议创建一个 WCF 服务来为您执行代码。 将服务设置为使用单向调用,并在页面上发起对服务的 Ajax 调用。
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.
据我所知,HttpWebRequest 对象的“BeginGetResponse”方法和“EndGetResponse”方法(通过调用 WebRequest.Create 获得)。 BeginGetRequestStream-/EndGetRequestStream 方法,这里尚未反映出来 - 尽管它们在文档中明确标记为“异步请求”:
不知道它是如何工作的。
如果要调用的页面位于同一个 IIS/App 上。 作为调用,您可以编写一个过滤器,如果映射字符串包含要调用的页面的规范(在 ISAPI-cpp 中:“return SF_STATUS_REQ_FINISHED;") 但也许被调用页面中的 asp/x/-script-execution 也被终止了。 检查问题。
考虑使用服务器端包含指令(条件较少)或使用 Server.Execute 动态调用 .asp 文件。
并不是真正的异步,但也许值得:通过 EARLY Response.End() (或类似的)在被调用的页面中向系统明确声明,不会有进一步的响应,主要是不会对调用者。 然后在 CALLED 页面的脚本中继续执行您的操作。 至少等待同步调用结束的时间可以最小化为 10、100 左右的 fac。
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:
No clue, how that works.
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.
Consider using server-side include directives (less conditionable) or dynamically call an .asp file by using Server.Execute.
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.
将
System.Net.WebClient.DownloadDataAsync/DownloadFileAsync
与DownloadDataCompleted/DownloadFileCompleted
结合使用。Use
System.Net.WebClient.DownloadDataAsync/DownloadFileAsync
in conjunction withDownloadDataCompleted/DownloadFileCompleted
.