一段时间以来,我一直在尝试解决这个问题,但我尝试过的一切都没有用。我尝试了 HttpClientHandler 但仍然出现错误!
错误信息:
SSL connection could not be established, see inner exception
Authentication failed, see inner exception
The the message received was unexpected or badly formatted
[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# 的,我是这门语言的初学者,所以请解释一下问题是什么。
这很可能是由于您定义和使用的 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);
}
这将以您之前期望的动态对象结束。
我是一名优秀的程序员,十分优秀!