.netxmlfetchmauissdp

Unable to Fetch XML in Android with SSDP in .NET MAUI


I am working on a .NET MAUI for Android project to discover devices on my local network using SSDP (Simple Service Discovery Protocol). I am able to successfully discover devices and retrieve their IPs and LOCATION headers. However, when I try to fetch the XML description document (to get the friendlyName), I consistently run into connection failures.

Here's what I am doing:

Using the Rssdp library to perform SSDP discovery. Fetching the DescriptionLocation from the discovered devices. Attempting to fetch the XML using HttpClient.

using System.Diagnostics;
using System.Net.Http;
using System.Xml.Linq;
using Rssdp;

public async void DiscoverDevices()
{
    using var deviceLocator = new SsdpDeviceLocator();
    var foundDevices = await deviceLocator.SearchAsync(); // Discover devices

    foreach (var device in foundDevices)
    {
        Debug.WriteLine($"Found device: {device.DescriptionLocation}");

        try
        {
            var fullDevice = await device.GetDeviceInfo(); // Full device info
            Debug.WriteLine($"Device Friendly Name: {fullDevice.FriendlyName}");
        }
        catch
        {
            // Fallback to manual HTTP fetch
            var xmlUrl = device.DescriptionLocation.ToString();
            Debug.WriteLine($"Fetching XML from: {xmlUrl}");
            
            try
            {
                using var httpClient = new HttpClient();
                var response = await httpClient.GetStringAsync(xmlUrl);

                var xml = XDocument.Parse(response);
                var ns = XNamespace.Get("urn:schemas-upnp-org:device-1-0");
                var friendlyName = xml.Descendants(ns + "friendlyName").FirstOrDefault()?.Value;

                Debug.WriteLine($"Fetched Friendly Name: {friendlyName}");
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"Failed to fetch XML: {ex.Message}");
            }
        }
    }
}

Questions:

Is there an inherent restriction in Android or .NET MAUI preventing the HTTP fetch for the XML?

Are there any alternative ways to fetch the friendly name when SSDP works but the XML fetch fails?

Could this be a limitation of the Rssdp library, and is there another library I should try?


Solution

  • If the DescriptionLocation provided use http you need set usesCleartextTraffic flag to true in order to GetDeviceInfo() to work.

    GetDeviceInfo() exactly do what you're doing in catch block, check here, So if the call to GetDeviceInfo() fails you fallback also fails.

    You can set usesCleartextTraffic in AndroidManifest.xml like below.

    <manifest xmlns:android="http://schemas.android.com/apk/res/android">
        <application android:allowBackup="true"  android:usesCleartextTraffic="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true"></application>
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        <uses-permission android:name="android.permission.INTERNET" />
    </manifest>