[英]Remote Server returns 404 not found error c#
我正在創建一個應用程序,用於檢查用戶狀態。 我收到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();
}
});
}
因為某種原因,您的用戶名是空白&
您的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":""}]}
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();
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.