HttpWebRequest in UWP (C#)

99封情书 提交于 2019-12-17 14:54:42

问题


I'm writing code to make HttpWebRequest to website

if website is working it will return HttpStatusCode.OK

if not it will return HttpStatusCode.NotFound

My code

 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();

But i have errors

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


回答1:


Although we can use HttpWebRequest Class in UWP apps, but not all of its methods. HttpWebRequest.GetResponse method and HttpWebResponse.Close Method are the methods that can't be used in UWP apps. Usually, we can find if a method can be used in UWP apps by checking the Version Information in the bottom of the document. If we can find Universal Windows Platform under Version Information then we should be able to use this method in UWP apps.

In .NET Core/UWP, System.Net.HttpWebRequest class is in System.Net.Requests library and is not recommended to use. Ref. .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.

And in UWP, we have two HttpClient APIs, they are System.Net.Http.HttpClient and Windows.Web.Http.HttpClient. You can choose either of them according to your requirement. For more info about these two HttpClient APIs, please see Demystifying HttpClient APIs in the Universal Windows Platform.



来源:https://stackoverflow.com/questions/38835974/httpwebrequest-in-uwp-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!