“WebRequest.Create(字符串)”已过时:“WebRequest,请改用 HttpClient。”
我将 .NET 应用程序从版本 NET5 升级到 NET6,并发出警告,指出 WebRequest 类已过时。我在网上查看了一些示例,但是使用 HttpClient 不会像 WebRequest 那样自动获取凭据,因为您需要手动将它们插入到字符串中。
我如何使用 HttpClient 类或类似的类来转换它?
string url = "website.com";
WebRequest wr = WebRequest.Create(url);
wr.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse hwr = (HttpWebResponse)wr.GetResponse();
StreamReader sr = new(hwr.GetResponseStream());
sr.Close();
hwr.Close();
I upgraded my .NET application from the version NET5 to NET6 and placed with a warning that the WebRequest class was obsolete. I've looked at a few examples online, but using HttpClient doesn't automatically grab credentials like WebRequest does, as you need to insert them into a string manually.
How would I convert this using the HttpClient class or similar?
string url = "website.com";
WebRequest wr = WebRequest.Create(url);
wr.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse hwr = (HttpWebResponse)wr.GetResponse();
StreamReader sr = new(hwr.GetResponseStream());
sr.Close();
hwr.Close();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尝试过以下方法吗?
有关凭据的更多信息:如何让 HttpClient随请求一起传递凭据?
Have you tried the following?
more info around credentials: How to get HttpClient to pass credentials along with the request?