Can't connect to external ip address via HTTPClient

267 views Asked by At

When i try to execute this MainPage.xaml.cs code:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Windows;
using System.Net.Http.Json;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Media;
using System.Net.Http;

namespace OpenSilverApplication3
{
    public partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            TestTod();
        }
        private async void TestTod()
        {
            using (HttpClient client = new HttpClient())
            {
                var response = await client.GetAsync("http://89.208.81.184:1337/DnD1C/hs/DNDAPI/V1/GETINFO");
                response.EnsureSuccessStatusCode();
                if (response.IsSuccessStatusCode)
                {
                    Testing.Text = await response.Content.ReadAsStringAsync();
                }
                else
                {
                    Testing.Text = $"Server error {response.StatusCode}";
                }
            }
        }
    }
}

MainPage.xaml code:

<sdk:Page
    x:Class="OpenSilverApplication3.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
    xmlns:local="clr-namespace:OpenSilverApplication3"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
   mc:Ignorable="d" 
      
    Title="Page1" Background="White" >
    <ScrollViewer HorizontalScrollBarVisibility="auto" VerticalScrollBarVisibility="auto">
        <Grid>
            <TextBlock x:Name="Testing" Width="450" Height="250" Text="Test" HorizontalAlignment="Center" VerticalAlignment="Bottom" Background="#FFD9D9D9" TextWrapping="Wrap"/>
        </Grid>
    </ScrollViewer>
    
</sdk:Page>

nothing happens.

If i enter this link("http://89.208.81.184:1337/DnD1C/hs/DNDAPI/V1/GETINFO") in browser, then page opens with data in JSON format. The database I'm connecting to is on a private server, so it doesn't have a domain name. This server use Apache 2.4.

I replace link("http://89.208.81.184:1337/DnD1C/hs/DNDAPI/V1/GETINFO") on this link("https://jsonplaceholder.typicode.com/todos") and then code execute correctly.

I use VS 2022. Using OpenSilver(https://opensilver.net/) Maybe I need give permissions to the application for connect to external ip-addresses?

I test this link("http://89.208.81.184:1337/DnD1C/hs/DNDAPI/V1/GETINFO") in fiddler. Result

Test with this link("http://89.208.81.184:1337/DnD1C/hs/DNDAPI/V1/GETINFO") Result

Test with this link("https://jsonplaceholder.typicode.com/todos") Result

I changed code to catch error.

namespace OpenSilverApplication3
{
    public partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            TestTod();
        }
        private async void TestTod()
        {
            using (HttpClient client = new HttpClient())
            {
                try
                {
                    var response = await client.GetAsync("http://89.208.81.184:1337/DnD1C/hs/DNDAPI/V1/GETINFO");
                    response.EnsureSuccessStatusCode();
                    if (response.IsSuccessStatusCode)
                    {
                        Testing.Text = await response.Content.ReadAsStringAsync();
                    }
                    else
                    {
                        Testing.Text = $"Server error {response.StatusCode}";
                    }
                }
                catch (Exception ex)
                {
                    Testing.Text = ex.ToString();
                }
            }
        }
    }
}

Here is the error I got:

System.Net.Http.HttpRequestException: TypeError: Failed to fetch
 ---> System.Runtime.InteropServices.JavaScript.JSException: TypeError: Failed to fetch
   at System.Net.Http.BrowserHttpHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at System.Net.Http.BrowserHttpHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
   at OpenSilverApplication3.MainPage.TestTod() in C:\Users\NyComp\source\repos\OpenSilverApplication3\OpenSilverApplication3\MainPage.xaml.cs:line 30
1

There are 1 answers

0
Click Ok On

This error message probably has to do with CORS. I know, the error message does not help... you can see more details here: Getting 'TypeError: Failed to fetch' error when trying to call AspNetCore Restful API from Blazor Wasm