测试不和谐邀请链接是否无效?

时间:2018-10-05 01:30:47

标签: c# discord

我正在尝试制作测试不一致邀请链接并查看它们是否无效的应用程序。但是我不知道如何检查。

1 个答案:

答案 0 :(得分:1)

使用对invite endpoint的GET请求。


请求:

GET: https://discordapp.com/api/invite/obviously-invalid-invite-code

响应(HTTP状态404):

{
    "code": 10006,
    "message": "Unknown Invite"
}

您可以通过以下方式执行此操作:对端点使用WebRequest调用,并捕获API返回404时抛出的WebException

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