在代码中从 c# 调用网页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/302775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call a webpage from c# in code
提问by TheAlbear
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
即使它设置为异步,它仍然会挂起,因为页面在线程完成之前不会完成渲染
采纳答案by TheAlbear
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,几乎在那里,但是每次我将任何请求放入单独的线程中时,页面仍然不会呈现,直到所有线程都完成运行。
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!!
}
回答by Joel Coehoorn
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()
方法发送请求而不阻塞应用程序的其余部分。
回答by Patrick Desjardins
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:
对于没有答案的 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();
}
回答by baretta
Use System.Net.WebClient.DownloadDataAsync/DownloadFileAsync
in conjunction with DownloadDataCompleted/DownloadFileCompleted
.
使用System.Net.WebClient.DownloadDataAsync/DownloadFileAsync
会同DownloadDataCompleted/DownloadFileCompleted
。
回答by ckramer
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 类具有用于在请求完成时通知调用者的事件,但由于您不关心它,因此不应有任何其他内容。
回答by Chris Marisic
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 服务来为您执行代码。将服务设置为使用 1 路调用,并在页面上启动对服务的 Ajax 调用。
回答by dos
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.
据我所知,HttpWebRequest 对象的“BeginGetResponse”方法和“EndGetResponse”方法(通过调用 WebRequest.Create 获得),分别是。BeginGetRequestStream-/EndGetRequestStream 方法,此处尚未反映,尽管它们在文档中被明确标记为“异步请求”:
不知道,这是如何工作的。
如果要调用的页面在同一个 IIS/App 上。作为调用,您可以编写一个过滤器,如果映射字符串包含要调用的页面的规范(在 ISAPI-cpp 中:“返回SF_STATUS_REQ_FINISHED;") 但也许你在被调用页面中的 asp/x/-script-execution 也被杀死了。检查问题。
考虑使用服务器端包含指令(条件较少)或使用 Server.Execute 动态调用 .asp 文件。
不是真正的异步,但可能是值得的:通过 EARLY Response.End()(或类似的)在被调用的页面中向系统明确说明,不需要进一步的响应,主要不是对调用者。然后在 CALLED 页面的脚本中执行您的工作。至少等待同步调用结束的时间间隔可以通过 10、100 左右的 fac 最小化。