[英]C# HttpWebRequest & HttpWebResponse
I have a question/I need help, I'm trying to make a "updater" for my C# program, and I'm always getting this error 我有一个问题/我需要帮助,我正在尝试为我的C#程序制作一个“更新程序”,我总是得到这个错误
Cannot implicitly convert type 'System.Net.WebRequest' to 'System.Net.HttpWebRequest'.
无法将类型'System.Net.WebRequest'隐式转换为'System.Net.HttpWebRequest'。 An explicit conversion exists (are you missing a cast?)
存在显式转换(您是否错过了演员?)
I was trying to make this "updater" as similar as possible to my .vb "updater", if anyone could help me solve this error I would be very thankful and happy, or if someone can send me a link to read about C# etc. I would also be very thankful, I'm very new to C# or the C family as well 我试图使这个“更新程序”尽可能与我的.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;
Thanks in advance! 提前致谢!
You have to use the System.Net.WebRequest.Create
method and cast the returned instance to System.Net.HttpWebRequest
: 您必须使用
System.Net.WebRequest.Create
方法并将返回的实例System.Net.HttpWebRequest
为System.Net.HttpWebRequest
:
HttpWebRequest myReq =
(HttpWebRequest)WebRequest.Create("http://www.contoso.com/");
See: https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest(v=vs.110).aspx#Anchor_7 请参阅: https : //msdn.microsoft.com/en-us/library/system.net.httpwebrequest(v = vs.110).aspx#Anchor_7
HttpWebRequest.Create()
is actually WebRequest.Create()
, which returns a WebRequest
. HttpWebRequest.Create()
实际上是WebRequest.Create()
,它返回一个WebRequest
。 You can cast it to HttpWebRequest
if needed. 如果需要,您可以将其
HttpWebRequest
为HttpWebRequest
。
If you look at documentation for WebRequest.Create
you will see that return type of the method is WebRequest
, so you need to return it in your code: 如果查看
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.