![](/img/trans.png)
[英]Match an asynchronous HttpWebResponse to a HttpWebRequest in C#
[英]C# HttpWebRequest & HttpWebResponse
我有一个问题/我需要帮助,我正在尝试为我的C#程序制作一个“更新程序”,我总是得到这个错误
无法将类型'System.Net.WebRequest'隐式转换为'System.Net.HttpWebRequest'。 存在显式转换(您是否错过了演员?)
我试图使这个“更新程序”尽可能与我的.vb“更新程序”相似,如果有人能帮助我解决这个错误我会非常感激和快乐,或者如果有人可以给我发送一个关于C#等的链接我也非常感激,我对C#或C家族也很陌生
System.Net.HttpWebRequest request = System.Net.HttpWebRequest.Create("link");
System.Net.HttpWebResponse response = request.GetResponse();
System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream());
string newestversion = sr.ReadToEnd();
string currentversion = Application.ProductVersion;
提前致谢!
您必须使用System.Net.WebRequest.Create
方法并将返回的实例System.Net.HttpWebRequest
为System.Net.HttpWebRequest
:
HttpWebRequest myReq =
(HttpWebRequest)WebRequest.Create("http://www.contoso.com/");
请参阅: https : //msdn.microsoft.com/en-us/library/system.net.httpwebrequest(v = vs.110).aspx#Anchor_7
HttpWebRequest.Create()
实际上是WebRequest.Create()
,它返回一个WebRequest
。 如果需要,您可以将其HttpWebRequest
为HttpWebRequest
。
如果查看WebRequest.Create
文档,您将看到该方法的返回类型是WebRequest
,因此您需要在代码中返回它:
System.Net.WebRequest request = System.Net.HttpWebRequest.Create("http://www.google.com");
System.Net.WebResponse response = request.GetResponse();
System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream());
string newestversion = sr.ReadToEnd();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.