我做了一個簡單的html測試頁。如果頁面被打開(http://localhost/cut/public/updateFile
),那麼我發送一個時間戳到我的網絡服務器,其中值被寫入一個文件date.txt
例如:C#Windows窗體應用程序 - 通過打開html發送請求
2017年4月8日14點50分19秒@
我還寫了一個C#Windows窗體應用程序,它向Web服務器發出請求,以每X秒獲取此文件的值(date.txt)。
我的目標是從文件中讀取到服務器上(僅適用於實踐)
但是每X秒,以顯示在通知中的當前時間,之前我得到的文件內容,我需要更新它首先得到當前的日期和時間。
有沒有可能用上面定義的規則解決這個問題? 這是我的嘗試:
private void timerA_Tick(object sender, EventArgs e)
{
sendWebRequest("http://localhost/cut/public/updateFile");
timerA.Stop();
timerA2.Interval = 5000;
timerA2.Start();
}
private void timerA2_tick(object sender, EventArgs e)
{
//Returns the content of date.txt
string response = sendWebRequest("http://localhost/cut/public/fileApi?action=read&targetFile=date");
//Show Notification
notifyIcon1.Visible = true;
notifyIcon1.Icon = SystemIcons.Exclamation;
notifyIcon1.BalloonTipTitle = "File content";
notifyIcon1.BalloonTipText = response;
notifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
notifyIcon1.ShowBalloonTip(10000);
timerA2.Stop();
timerA.Start();
}
/**
* Send request and get response as string
*/
public static string sendWebRequest(string URL)
{
WebRequest request = WebRequest.Create(URL);
WebResponse response = request.GetResponse();
StreamReader streamReader = new StreamReader(response.GetResponseStream());
return (string)streamReader.ReadToEnd();
}
不過,我總是得到4/8/2017 @ 14:50:19
它不更新,因爲它應該。
它顯然不會這樣工作,因爲WebRequest.Create
只會獲取html文件,並將其傳回給我的C#應用程序,但它不會執行JavaScript,因爲我向服務器發出請求。
我只是在上面創建了這個例子,以問它是否有可能通過這種方式實現這一點,或者如果C#沒有被設計來解決像這樣的問題?
我唯一的想法是建立在我的Form1中隱藏的網頁瀏覽器和開放http://localhost/cut/public/updateFile開始更新,但我不知道這甚至工程。
我創建了一個網頁瀏覽器元素,並調用更新網址是這樣的:
webBrowser1.Navigate("http://localhost/cut/public/updateFile");
然而,也有很多腳本錯誤消息,這是在jQuery的文件中找到的。
而且我的腳本也不會因爲不工作的jQuery的工作。 所以我想這會工作,但不是與jQuery,或者我必須修復jQuery文件中的所有錯誤。
如何解決這個問題?
'webBrowser1.ScriptErrorsSuppressed = TRUE;' - 設置此和再試一次 - https://stackoverflow.com/questions/2476360/disable-javascript-error-in-webbrowser-control – Subbu
@Subbu,已經嘗試過,但它沒有幫助,因爲腳本仍然沒有工作。 – Black