简体   繁体   English

收到401未经授权的错误

[英]401 Unauthorised Error Received

I am using the habbo api to check if a name is valid. 我正在使用habbo api检查名称是否有效。 I'm receiving a 401 Unauthorised Error. 我收到401未经授权的错误。

Below is the code I'm using. 下面是我正在使用的代码。 It worked when I copied my Cookie header in chrome and added that as a header. 当我将我的Cookie标头复制到chrome中并将其添加为标头时,此方法就起作用了。 But is there another way and an actual fix? 但是还有其他方法和实际解决方法吗?

    private void Form1_Load(object sender, EventArgs e)
    {
        try
        {
            using(WebClient WebClient = new WebClient())
            {
                WebClient.Headers.Add("User-Agent", "Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30");
                MessageBox.Show(WebClient.DownloadString("https://www.habbo.com/api/user/avatars/check-name?name=123"));
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

The API https://www.habbo.com/api/user/avatars/check-name what you are referring wont load without proper authorization token, since that one is not public available. 没有正确的授权令牌,API https://www.habbo.com/api/user/avatars/check-name所引用的内容将无法加载,因为该令牌不是公共可用的。

To further test, use the public API https://www.habbo.com/api/public/users?name= 要进行进一步测试,请使用公共API https://www.habbo.com/api/public/users?name=

You will be able to get response without any issues. 您将能够毫无问题地得到回应。

The 401 error indicates that you need to add (basic) authentication to your HTTP request (and remove the cookie that you added): 401错误表示您需要向HTTP请求中添加(基本)身份验证(并删除添加的cookie):

String username = “username”;
String password = “password”;

String credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(username + “:” + password));
WebClient.Headers[HttpRequestHeader.Authorization] = “Basic ” + credentials;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM