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
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:
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.