Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
554 views
in Technique[技术] by (71.8m points)

visual studio - HttpWebRequest in UWP (C#)

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 emesDocumentsGitHubMilano_pizzaMilanoMainPage.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 emesDocumentsGitHubMilano_pizzaMilanoMainPage.xaml.cs 59 Active

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...