C#Windows Forms App-通过打开html发送请求 [英] C# Windows Forms App - send request by opening html
问题描述
我做了一个简单的html测试页.如果打开了页面( http://localhost/cut/public/updateFile
),则我将时间戳发送到我的Web服务器,其中该值被写入文件 date.txt 代码>例如:
2017年4月8日下午14:50:19
我还编写了一个C#Windows Forms App,它每隔X秒向Web服务器发出一个请求,以获取此文件(date.txt)的值.
我的目标是通过从服务器上的文件中读取当前时间,每隔X秒在通知框中显示一次当前时间(仅供练习)
但是,在获取文件内容之前,我当然需要先对其进行更新以获取当前日期和时间.
是否可以使用上面定义的规则解决此问题?这是我的尝试:
private void timerA_Tick(对象发送者,EventArgs e){sendWebRequest("http://localhost/cut/public/updateFile");timerA.Stop();timerA2.Interval = 5000;timerA2.Start();}private void timerA2_tick(对象发送者,EventArgs e){//返回date.txt的内容字符串响应= sendWebRequest("http://localhost/cut/public/fileApi?action = read& targetFile = date");//显示通知notifyIcon1.Visible = true;notifyIcon1.Icon = SystemIcons.Exclamation;notifyIcon1.BalloonTipTitle =文件内容";notifyIcon1.BalloonTipText =响应;notifyIcon1.BalloonTipIcon = ToolTipIcon.Info;notifyIcon1.ShowBalloonTip(10000);timerA2.Stop();timerA.Start();}/***发送请求并以字符串形式获取响应*/公共静态字符串sendWebRequest(字符串URL){WebRequest request = WebRequest.Create(URL);Web响应响应= request.GetResponse();StreamReader streamReader =新的StreamReader(response.GetResponseStream());返回(string)streamReader.ReadToEnd();}
但是我总是得到 4/8/2017 @ 14:50:19
,它没有按应有的方式更新.
由于 WebRequest.Create
仅按原样获取html文件并将其传递回我的C#应用程序,但是它不执行我在其中执行的javascript,因此显然不能这样工作向服务器的请求.
我只是在上面创建了这个示例,询问是否有可能以这种方式实现这一目标,或者C#并非旨在解决此类问题?
我唯一的想法是在Form1中创建一个隐藏的Web浏览器,然后打开
我创建了一个webbrowser元素,并这样调用更新URL:
webBrowser1.Navigate("http://localhost/cut/public/updateFile");
但是,在jquery文件中有很多脚本错误消息.
由于无法使用jquery,我的脚本也无法使用.因此,我想它会起作用,但不适用于jquery,否则我必须修复jQuery文件中的所有错误.
如何解决这个问题?
我通过在表单中添加一个webbrowser元素并在其中调用URL来解决了这个问题:
webBrowser1.Navigate("http://localhost/cut/public/updateFile");
但是,由于C#Webbrowser似乎是一个极端的Internet Explorer,不支持任何功能,因此我不得不重写JavaScript以使其在没有jQuery的情况下也可以工作.有很多警报指向脚本错误,如下所示:
现在,它可以按预期工作.我希望总有一天会有比这更好的解决方案.
更新
通过将以下行添加到html头部分中,我能够将浏览器更改为最新版本:
<元http-equiv ="X-UA-Compatible" content ="IE = edge"/><!-IE = edge指示WebBrowser控件使用它支持的最新版本的IE->
I made a simple html testpage. If the page is opened (http://localhost/cut/public/updateFile
) then I send a timestamp to my webserver, where the value is written into a file date.txt
e.g.:
4/8/2017 @ 14:50:19
I also wrote a C# Windows Forms App which makes a request to the webserver to get the value of this file (date.txt), every X seconds.
My goal is to show the current time in a notification box every X seconds by reading it from the file on the server (for practice only)
However, before I get the file content, I need to update it first of course to get the current date and time.
Is it possible to solve this with the rules defined above? This is my attempt:
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();
}
However I always get 4/8/2017 @ 14:50:19
it does not update as It should.
It obviously does not work this way, since WebRequest.Create
does only gets the html file as it is and delivers it back to my C# Application, but it does not execute the javascript where I make the request to the server.
I only created this example above to ask if it is somehow possible to achive this at this way or if C# is not designed to solve problems like this?
My only idea is to create a hidden webbrowser in my Form1 and open http://localhost/cut/public/updateFile to start the update but I am not sure if this even works.
I created a webbrowser element and call the update URL like this:
webBrowser1.Navigate("http://localhost/cut/public/updateFile");
However, there are plenty of script error messages, which are found in the jquery file.
And my script won't work either because of the not working jquery. So I guess it would work, but not with jquery, or I have to fix all errors in the jQuery file.
How to solve this problem?
I solved it by adding a webbrowser element to my Form, and calling the URL inside of it like this:
webBrowser1.Navigate("http://localhost/cut/public/updateFile");
However, I had to rewrote my javascript to make it work without jQuery, since the C# Webbrowser appears to be an extremly old Internet Explorer with no support for nothing. There were plenty alerts pointing to script errors like the one below:
Now it works as expected. I hope someday somebody will come across with a much better solution than this though.
UPDATE
I was able to change the browser to the latest available by adding this line to my html head section:
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<!-- IE=edge instructs the WebBrowser control to use the latest version of IE it supports -->
这篇关于C#Windows Forms App-通过打开html发送请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!