【问题标题】:Remote Server returns 404 not found error c#远程服务器返回 404 not found 错误 c#
【发布时间】:2018-04-17 19:55:36
【问题描述】:

我正在创建一个应用程序来检查用户的状态。我收到 404 未找到错误。任何人都可以帮助代码发布在下面。我做错了什么?

 static void Main(string[] args)
    {
        var Usernames = File.ReadAllLines(@"C:\Users\Hasan\Desktop\Usernames.txt");
        Parallel.ForEach(Usernames, Username =>
        {
            try
            {
                using (WebClient WebClient = new WebClient())
                {
                    WebClient.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.75 Safari/537.36");
                    string response = WebClient.DownloadString("https://www.habbo.com/api/public/users?name=" + Username);
                    if(response.Contains("not - found"))
                    {
                        Console.WriteLine("Possibly Free : " + Username);
                    }
                }
            }
            catch (Exception ex)
            {
                //Console.WriteLine("Error on Username : " + Username);
                Console.WriteLine(ex.Message);
                Console.ReadLine();
            }
        });
    }

【问题讨论】:

  • 当您从浏览器浏览相同的 url 时会发生什么?
  • @Hassan 如果您的问题得到解决...标记答案并关闭此线程

标签: c# api http-status-code-404


【解决方案1】:

由于某种原因,您的 用户名 为空且

你的 DownloadString 变成了

https://www.habbo.com/api/public/users?name=

因此出现 404 错误..

尝试在这一行放一个调试器

string response = WebClient.DownloadString("https://www.habbo.com/api/public/users?name=" + Username);

你会看到一个空白的用户名..

因为 .. 我检查了 Api 对于像这样的有效用户名是否正常工作

https://www.habbo.com/api/public/users?name=trump

它返回一个有效的 JSON

{"uniqueId":"hhus-04bce17a17b59979fbd97aa46110a650","name":"Trump","figureString":"hr-835-1402.hd-627-13.ch-3005-1326-97.lg-3483-1415-1415.he-3227-95.fa-3276-1328","motto":"covfefe","memberSince":"2011-05-11T22:20:28.000+0000","profileVisible":true,"selectedBadges":[{"badgeIndex":1,"code":"ACH_RegistrationDuration20","name":"100 % True Habbo XX","description":"Be a member of the community for 1825 days."},{"badgeIndex":2,"code":"UK824","name":"You took a quack at the duck games! (And won!)","description":""},{"badgeIndex":3,"code":"UK833","name":"Singapores National Flower","description":""},{"badgeIndex":4,"code":"UK835","name":"I picked a Lignum Vitae on Jamaica Day 2017!","description":""},{"badgeIndex":5,"code":"UK838","name":"Pretty Polly want a cracker?!","description":""}]}

【讨论】:

  • 刚刚检查它返回 404 对于不存在或被禁止的用户名
  • 好吧,我的错..我重新检查并发现 对于不存在或被禁止的用户名返回 404 你是对的 :)
  • 谢谢老哥,我想看看哪些名字可以免费注册,有点烦人
  • 你可以随时检查任何想要的名称...&如果名称返回 404 ...很可能它可用
  • 接受这个作为答案...如果这篇文章帮助您解决代码问题:)
【解决方案2】:

webapi 为每个未知用户返回 404 错误。在 404 响应时,Web 客户端会引发异常,因此您需要将您的验证调整为以下内容:

static void Main(string[] args)
{
    var Usernames = File.ReadAllLines(@"C:\Users\Hasan\Desktop\Usernames.txt");
    Parallel.ForEach(Usernames, Username =>
    {
        try
        {
            using (WebClient WebClient = new WebClient())
            {
                WebClient.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.75 Safari/537.36");
                string response = WebClient.DownloadString("https://www.habbo.com/api/public/users?name=" + Username);
        }
        catch (Exception ex)
        {
            if ( ex is WebException and (ex.Response as HttpWebResponse)?.StatusCode.ToString() ?? ex.Status.ToString() == "404" )
            {
                Console.WriteLine("Possibly Free : " + Username);
            }
            //Console.WriteLine("Error on Username : " + Username);
            Console.WriteLine(ex.Message);
            Console.ReadLine();
        }
    });
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-20
    • 1970-01-01
    • 2014-01-19
    • 2019-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-03
    相关资源
    最近更新 更多