我正在创建一个应用程序,它会检查以查看用户的状态。我收到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();
}
});
}
答案 0 :(得分:1)
由于某些原因, 用户名 为空白&
您的DownloadString变为
因此404错误..
尝试将调试器放在此行
string response = WebClient.DownloadString("https://www.habbo.com/api/public/users?name=" + Username);
你会看到一个空白的用户名..
因为..我检查过Api对于有效的用户名(如
)工作正常并返回有效的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":""}]}
答案 1 :(得分:0)
webapi为每个未知用户返回404错误。在404响应时,webclient会抛出异常,因此您需要根据以下内容调整验证:
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();
}
});
}