status code Forbidden

Eduardo Gomez Romero 705 Reputation points
2024-09-24T16:10:26.4766667+00:00

hello I have a little problem

So am calling an Api, I made a service

    public class HttpService(HttpClient httpClient, IConnectivity connectivity) {

        public async Task<T?> GetAsync<T>(string url) {

            if (connectivity.NetworkAccess != NetworkAccess.Internet) {

                await Shell.Current.DisplayAlert("Error",
                    "No internet connectivity"
                    , "OK");

                return default;

            }

            // Add User-Agent header
            httpClient.DefaultRequestHeaders.Add
                ("User-Agent",
                "Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; Googlebot/2.1; +http://www.google.com/bot.html)");

            var response =
                await httpClient.GetAsync(url);

            if (response.IsSuccessStatusCode) {
                var jsonData = await response.Content.ReadAsStringAsync();

                var data = await response.Content.ReadFromJsonAsync<T>();
                return data;

            } else {

                await Shell.Current.DisplayAlert("Error",
                    "We could not connect to the server", "OK");

            }

            return default;
        }
    }
}

So the app runs fine in android,

but as you can see, the image doesn't render in Widows, and I get the error

	Microsoft.Maui.Controls.StreamWrapper: Warning: Could not retrieve https://www.sciencedaily.com/images/scidaily-icon.png, status code Forbidden

This only happens on windows

Maui version: 8.0.9

Affected platforms: Windows

Expected behavior: Show the image

Actual behavior: No image

Testing system: Windows 11

Net 8

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,541 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 76,001 Reputation points Microsoft Vendor
    2024-09-25T05:32:15.5166667+00:00

    ===========Update============

    You can test this <Image Source="https://aka.ms/campus.jpg" /> code. It works for all platforms.

    I tested your url, it can be loaded in the Webview normally, but sometimes users will have security check.

    HTTP 403 - Forbidden means that the server understood the request, but server rejected it because the caller didn't have access. 

    This is a server-side limitation or policy about accessing the source.

    By the way, for protecting your application, I removed unrelated VM code, images, and xaml.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.