无法通过 HTTPClient 连接到外部 IP 地址

问题描述 投票:0回答:1

当我尝试执行此操作时 MainPage.xaml.cs代码:

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代码:

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

什么也没发生。

如果我在浏览器中输入此链接(“http://89.208.81.184:1337/DnD1C/hs/DNDAPI/V1/GETINFO”),则页面将打开,其中包含 JSON 格式的数据。我连接的数据库位于私人服务器上,因此它没有域名。该服务器使用 Apache 2.4。

我替换此链接(“https://jsonplaceholder.typicode.com/todos”)上的链接(“http://89.208.81.184:1337/DnD1C/hs/DNDAPI/V1/GETINFO”),然后代码正确执行.

我使用 VS 2022。使用 OpenSilver(https://opensilver.net/) 也许我需要授予应用程序连接到外部 IP 地址的权限?

我在fiddler中测试了这个链接(“http://89.208.81.184:1337/DnD1C/hs/DNDAPI/V1/GETINFO”)。 Result

使用此链接进行测试(“http://89.208.81.184:1337/DnD1C/hs/DNDAPI/V1/GETINFO”) Result

使用此链接进行测试(“https://jsonplaceholder.typicode.com/todos”) Result

我更改了代码以捕获错误。

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();
                }
            }
        }
    }
}

这是我得到的错误:

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
c# dotnet-httpclient opensilver
1个回答
0
投票

此错误消息可能与 CORS 有关。我知道,错误消息没有帮助...您可以在此处查看更多详细信息:尝试从 Blazor Wasm 调用 AspNetCore Restful API 时出现“TypeError:无法获取”错误

© www.soinside.com 2019 - 2024. All rights reserved.