What exception is thrown by ReadBufferAsync when there is no internet connectivity

-1

When getting data from the net, how can I tell if I failed to connect? I am using GetBufferAsync, which throws an error when I pull the ethernet cable out of the computer.
I can find no documentation about this error, and I don't know if this is the best method to use. I am loading a text file that is up to 60Kb. Not very much data.

I am developing in Visual Studio 2019.

The code below loads some data from www.google.com, mostly 0's. When I disconnect the ethernet cable, I get exception thrown at the ReadBufferAsync line. I tried catching specific exceptions: InvalidOperationException, HttpRequestException

Error: 0x8002ee7 (decimal 12007).
Text: The text associated with this error code could not be found. The server name or address could not be resolved.
InnerException: null

The MS documentation https://docs.microsoft.com/en-us/windows/desktop/WinInet/wininet-errors does not include the error I'm getting, though my exception is in the correct range (see https://docs.microsoft.com/en-us/windows/desktop/Debug/system-error-codes--12000-15999-)

using System;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.Web.Http;


namespace ReadBufferAsyncTest
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();

            this.Loaded += MyProgram_Loaded;
        }

        private async void MyProgram_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                // Connecting to internet
                var uriBing = new Uri("http://www.google.com");
                var client = new HttpClient();
                IBuffer morseBuffer = await client.GetBufferAsync(uriBing);
                DataReader dataReader = DataReader.FromBuffer(morseBuffer);
                byte[] morseBytes = new byte[morseBuffer.Length];
                dataReader.ReadBytes(morseBytes);
            }
            catch(Exception ex)
            {
                return;      // breakpoint goes here
            }
        }
    }
}

Is the error I am getting (0x80072ee7) the expected error? Are there other errors to look for? What is the best way to handle the errors when there is limited documentation?

(other note: is this the best way to download a small text file?)

c#
exception
uwp
asked on Stack Overflow Jun 18, 2019 by Bob Krauth

1 Answer

0

how can I tell if I failed to connect?

For your requirement, you could check if the internet is available before getting buffer via NetworkHelper.

// Detect if Internet can be reached
if (NetworkHelper.Instance.ConnectionInformation.IsInternetAvailable)
{
}
answered on Stack Overflow Jun 19, 2019 by Nico Zhu - MSFT • edited Jun 20, 2019 by Nico Zhu - MSFT

User contributions licensed under CC BY-SA 3.0