gpt4 book ai didi

c# - UWP 中的 HttpWebRequest (C#)

转载 作者:行者123 更新时间:2023-12-02 12:15:10 26 4
gpt4 key购买 nike

我正在编写代码来向网站发出 HttpWebRequest

如果网站正在运行,它将返回HttpStatusCode.OK

如果没有,它将返回HttpStatusCode.NotFound

我的代码

 var url = "http://simplegames.com.ua/";

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
Debug.WriteLine("All ok");
}
else if (response.StatusCode == HttpStatusCode.NotFound)
{
Debug.WriteLine("URL not working");
}
response.Close();

但是我有错误

1) Severity Code Description Project File Line Suppression State Error CS1061 'HttpWebRequest' does not contain a definition for 'GetResponse' and no extension method 'GetResponse' accepting a first argument of type 'HttpWebRequest' could be found (are you missing a using directive or an assembly reference?) Milano C:\Users\nemes\Documents\GitHub\Milano_pizza\Milano\MainPage.xaml.cs 50 Active

2) Severity Code Description Project File Line Suppression State Error CS1929 'HttpWebResponse' does not contain a definition for 'Close' and the best extension method overload 'ExtensionMethods.Close(Stream)' requires a receiver of type 'Stream' Milano C:\Users\nemes\Documents\GitHub\Milano_pizza\Milano\MainPage.xaml.cs 59 Active

最佳答案

虽然我们可以使用HttpWebRequest Class在 UWP 应用中,但不是其所有方法。 HttpWebRequest.GetResponse methodHttpWebResponse.Close Method是无法在 UWP 应用中使用的方法。通常,我们可以通过查看文档底部的版本信息来了解某个方法是否可以在UWP应用中使用。如果我们可以在版本信息下找到通用Windows平台,那么我们应该能够在UWP应用中使用此方法。

在.NET Core/UWP中,System.Net.HttpWebRequest类位于System.Net.Requests库中,不建议使用。引用号.NET Networking APIs for UWP Apps :

System.Net.Requests

This library contains types related to System.Net.HttpWebRequest and System.Net.HttpWebResponse classes that allow developers to implement the client role of the HTTP protocol. The API surface for .NET Core 5 is the same as that available for Windows 8.1 apps and is very limited compared to the surface in the .NET Framework. This is intentional and we highly encourage switching to the HttpClient API instead – that is where our energy and innovation will be focused going forward.

This library is provided purely for backward compatibility and to unblock usage of .NET libraries that use these older APIs. For .NET Core, the implementation of HttpWebRequest is actually based on HttpClient (reversing the dependency order from .NET Framework). As mentioned above, the reason for this is to avoid usage of the managed .NET HTTP stack in a UWP app context and move towards HttpClient as a single HTTP client role API for .NET developers.

在 UWP 中,我们有两个 HttpClient API,它们是 System.Net.Http.HttpClient Windows.Web.Http.HttpClient 。您可以根据您的要求选择其中之一。有关这两个 HttpClient API 的更多信息,请参阅Demystifying HttpClient APIs in the Universal Windows Platform .

关于c# - UWP 中的 HttpWebRequest (C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38835974/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com