【问题标题】:How to fix 'The the message received was unexpected or badly formatted'如何解决“收到的消息意外或格式错误”
【发布时间】:2019-08-27 19:18:32
【问题描述】:

我已经尝试解决这个问题一段时间了,但我尝试过的一切都是无用的。我尝试了 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# 的,而且我是这门语言的初学者,所以请解释一下问题所在。

【问题讨论】:

  • 错误发生在哪一行?
  • 我不知道
  • 我认为它发生在字符串 osuProf = await client.GetStringAsync(url);行,因为我也在其他命令上使用它,并且也发生了
  • 不完整的错误信息给你一个行号吗?
  • 不,它没有给我行号imgur.com/a/ens4bd7

标签: c# .net ssl httpclient discord.net


【解决方案1】:

这很可能是由于您定义和使用的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);
}

最终会得到您之前期望的动态对象。

【讨论】:

  • @Citrom 你确定你使用的是正确的key吗?
  • 是的,我使用了正确的密钥,但奇怪的是与此错误没有一致性,因为大多数情况下它都可以正常工作,但 50 次中有 1 次是随机的SSL 错误,然后它就像什么都没发生一样工作
  • 听起来这很可能是您的互联网问题,或者是防火墙、出站速率限制等相关配置。
  • 这就是我所害怕的!无论如何感谢您的帮助!我试图找出造成这种情况的原因
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-21
  • 1970-01-01
  • 2014-05-31
  • 1970-01-01
  • 1970-01-01
  • 2011-04-28
  • 1970-01-01
相关资源
最近更新 更多