我正在嘗試建立一個網站上下檢查器。但是當我嘗試運行我的程式時,我得到了這個錯誤:(不能將運算子'!'應用于'string'型別的運算元)
我該如何解決?
我是 c# 的新手。
錯誤在 if 陳述句中。
這是代碼:
using System;
using System.Net;
namespace cc
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter Your Website for further checking: ");
string url = Console.ReadLine();
System.Threading.Thread.Sleep(3);
Console.WriteLine("Checking...");
System.Threading.Thread.Sleep(3);
Console.WriteLine("Found some shit.");
System.Threading.Thread.Sleep(3);
Console.WriteLine("Printing Request...");
WebClient client = new WebClient();
string checksite = client.DownloadString(url);
if (!checksite == HttpStatusCode.OK)
{
Console.WriteLine("Your Bitch is Down!");
}
}
}
}
uj5u.com熱心網友回復:
首先,(!string == othervalue)并不意味著你認為它做了什么。您正在對字串進行否定,這沒有任何意義,然后查看它是否等于另一側的值,因為運算符優先級!高于.==
但是,假設您解決了這個問題:要么A != B或!(A == B)將作業。你仍然有一個問題,因為資料型別很重要。檢查兩種不同物件的相等性幾乎沒有意義。
在這種情況下,DownloadString()根本不回傳 an HttpStatusCode。它從頁面提供的任何 HttpResponse 作為字串回傳正文,并為任何不是好的狀態代碼拋出例外。
所以你想要這個:
try
{
client.DownloadString(url);
}
catch(WebException ex)
{
Console.WriteLine("Your site is Down!");
}
uj5u.com熱心網友回復:
為了檢查字串是否不是“某物”,您必須()像這樣添加:
!(checksite == HttpStatusCode.OK)
或者
checksite != HttpStatusCode.OK
uj5u.com熱心網友回復:
您必須使用 != 比較。
目前,您嘗試否定字串本身,這是不可能的。
PS:我會將輸出更改為不那么令人反感的東西。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/524939.html
標籤:C#http
上一篇:EFCore-我是否必須有一個“使用”陳述句才能處理我的背景關系?
下一篇:如何重新排序陣列
