简体   繁体   English

如何在C#中实现Javascript的XmlHttpRequest?

[英]How to implement Javascript's XmlHttpRequest in C#?

There is an old article in the MSDN Magazine with a walk-through to implement in C# the equivalent of the Javascript XMLHttpRequest object. 《 MSDN杂志》上有一篇老文章 ,其中有一个演练,介绍了如何在C#中实现与Javascript XMLHttpRequest对象等效的方法。

I find it very interesting but the code is not downloadable anymore. 我觉得它很有趣,但是代码不再可下载。

Do you know of an implementation similar to the Javascript XMLHttpRequest object but in C#? 您是否知道在C#中类似于Javascript XMLHttpRequest对象的实现? Among other thinks, this implementation can: 除其他想法外,此实现可以:

  • Send http requests to the server. 将http请求发送到服务器。
  • Send requests both synchronously and asynchronously. 同步和异步发送请求。
  • Allows to abort long running processes. 允许中止长时间运行的进程。

I know it all can be done easily with the HTTPWebRequest object in .NET, but after reading the article I was hoping that a wrapper would be available as it used to be in the code of that article (the link is dead). 我知道使用.NET中的HTTPWebRequest对象可以轻松完成所有操作,但是在阅读了这篇文章之后,我希望可以使用包装程序,因为该包装曾经在该文章的代码中使用(链接已失效)。

Thanks, 谢谢,

看一下WebClient类

You can use WebClient.DownloadString that is a good wrapper around a web request. 您可以使用WebClient.DownloadString ,它是Web请求的良好包装。 You can have a non blocking call too ( the better way ) by using DownloadStringAsync 您也可以使用DownloadStringAsync进行非阻塞调用(更好的方法)

You can use WebClient, though I often find it wraps a bit too much and I end up going back to HTTPWebRequest. 您可以使用WebClient,尽管我经常发现它包装太多,最终回到HTTPWebRequest。

There's plenty of the code from that article in it's figures at http://msdn.microsoft.com/hr-hr/magazine/bb985675%28en-us%29.aspx Enough really to reconstruct the whole thing. 该文章中的文章中提供了大量代码, 网址http://msdn.microsoft.com/hr-hr/magazine/bb985675%28en-us%29.aspx确实足以重构整个事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM