【发布时间】:2017-08-05 01:59:06
【问题描述】:
我做了一个简单的 html 测试页。如果页面被打开(http://localhost/cut/public/updateFile),那么我会向我的网络服务器发送一个时间戳,其中值被写入文件date.txt,例如:
2017 年 4 月 8 日 @ 14:50:19
我还编写了一个 C# Windows 窗体应用程序,它每隔 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 以开始更新,但我不确定这是否有效。
我创建了一个 webbrowser 元素并像这样调用更新 URL:
webBrowser1.Navigate("http://localhost/cut/public/updateFile");
但是,有很多脚本错误消息,可以在 jquery 文件中找到。
而且我的脚本也不能工作,因为 jquery 不工作。 所以我想它会起作用,但不能使用 jquery,否则我必须修复 jQuery 文件中的所有错误。
如何解决这个问题?
【问题讨论】:
-
webBrowser1.ScriptErrorsSuppressed = true;- 设置并重试 - stackoverflow.com/questions/2476360/… -
@Subbu,已经尝试过了,但它没有帮助,因为脚本仍然无法正常工作。
标签: javascript c# html ajax