如何解决“收到的消息意外或格式错误”

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

我已经尝试解决这个问题有一段时间了,但我所尝试的一切都是无用的。我尝试了 HttpClientHandler 但仍然收到错误!

错误信息:

无法建立SSL连接,请参阅内部异常

认证失败,查看内部异常

收到的消息是意外的或格式错误

[Command("stats")] 
public async Task Profileosu([Remainder]string username = null)
{
    try
    {
        clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };

        HttpClient client = new HttpClient(clientHandler,disposeHandler: true);
        List<Player> player = new List<Player>();
        List<string> lines = File.ReadAllLines(path, encoding: Encoding.UTF8).ToList();
        string id = "";
        foreach (var line in lines)
        {
            string[] readed = line.Split(",");

            Player newPlayer = new Player();
            newPlayer.id = readed[0];
            newPlayer.osuname = readed[1];

            player.Add(newPlayer);
        }

        if (username is null)
        {
            id = Context.User.Id.ToString();
            username = Context.User.Username;
        }
        else if (Context.Message.MentionedUsers.Count > 0)
        {
            username = Context.Message.MentionedUsers.First().Username;
            id = Context.Message.MentionedUsers.First().Id.ToString();
        }
        for (int i = 0; i < player.Count(); i++)
        {
            if (player[i].id == id)
            {
                username = player[i].osuname;
            }
        }

        string url = $"https://osu.ppy.sh/api/get_user?k={k}&u={username}";
        string osuProf = await client.GetStringAsync(url);
        dynamic osuProfile = JsonConvert.DeserializeObject<dynamic>(value: osuProf);
        string pp_raw = osuProfile[0]["pp_raw"];
        string country = osuProfile[0]["country"];
        string user_id = osuProfile[0]["user_id"];
        string joinDate = osuProfile[0]["join_date"];
        string rank = osuProfile[0]["pp_rank"];
        string countryRank = osuProfile[0]["pp_country_rank"];
        string accuracy = osuProfile[0]["accuracy"];
        string playcount = osuProfile[0]["playcount"];
        string userName = osuProfile[0]["username"];



        embed.WithThumbnailUrl($"https://a.ppy.sh/{user_id}");
        embed.WithAuthor($"{username} #{rank}, {pp_raw}PP", Context.Guild.CurrentUser.GetAvatarUrl(), $"https://osu.ppy.sh/users/{user_id}");
        embed.WithDescription($"Join date:{joinDate}\nCountry:{country} #{countryRank}\n");
        embed.WithFooter($"Accuray:{double.Parse(accuracy):F2}%\t\tPlaycount:{playcount}");
        embed.WithColor(154, 255, 0);

        await ReplyAsync($"", false, embed.Build());
    }
    catch (Exception ex)
    {
        embed.WithAuthor("An error occurred");
        embed.WithDescription("This player doesn't exist! Please check the username and try again!");
        embed.WithColor(255, 0, 0);
        await ReplyAsync($"", false, embed.Build());
        Console.WriteLine(ex.Message);
        if (ex.InnerException != null)
        {
            Console.WriteLine(ex.InnerException.Message);
        }
        if (ex.InnerException.InnerException.Message != null)
        {
            Console.WriteLine(ex.InnerException.InnerException.Message);
        }    
    }
}

我从头开始学习 C#,我是这门语言的初学者,所以请解释一下问题是什么。

c# .net ssl httpclient discord.net
1个回答
0
投票

这很可能是由于您定义和使用的

clientHandler
导致的。

为了与 OSU API 进行通信,您也并不真正需要它。 因此,您可以继续让

HttpClient
为您处理此问题。

所以代替:

clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };

HttpClient client = new HttpClient(clientHandler,disposeHandler: true);

您可以按如下方式定义

HttpClient

HttpClient client = new HttpClient();

由于您现在不再定义 disposeHandler,因此最好将 Finally 添加到您的 try catch 中。

或者将

using
应用于
HttpClient

using (var client = new HttpClient())
{
    string url = $"https://osu.ppy.sh/api/get_user?k={Key}&u=d3ullist";
    string osuProf = await client.GetStringAsync(url);
    dynamic osuProfile = JsonConvert.DeserializeObject<dynamic>(value: osuProf);
}

最终将得到动态对象,正如您之前所期望的那样。

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