测试 Discord 邀请链接是否无效?

Red*_*eon 0 c# discord

我正在尝试制作一个应用程序来测试 Discord 邀请链接并检查它们是否无效或有效。但是,我不知道该怎么做。

Hop*_*nut 5

对邀请端点使用 GET 请求。


要求:

GET: https://discordapp.com/api/invite/obviously-invalid-invite-code
Run Code Online (Sandbox Code Playgroud)

响应(HTTP 状态 404):

{
    "code": 10006,
    "message": "Unknown Invite"
}
Run Code Online (Sandbox Code Playgroud)

您可以通过WebRequest调用端点并捕获WebExceptionAPI 返回 404 时抛出的异常来完成此操作。

try
{
    WebRequest request = WebRequest.Create("https://discordapp.com/api/invites/obviously-invalid-invite-code");
    request.Method = "GET";

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

    if (response.StatusCode == HttpStatusCode.OK) // and possibly other checks in the response contents
    {
        Console.WriteLine("Invite link is valid");
    }
} 
catch (WebException wex)
{
    if (((HttpWebResponse)wex.Response).StatusCode == HttpStatusCode.NotFound)
    {
        Console.WriteLine("Invite link is invalid");
    }
    // You may need to account for other 400/500 statuses
    else throw wex;
}
Run Code Online (Sandbox Code Playgroud)