I'm creating an application with C# that should should download data as text from a file (found online) at this location: http://asap.eb2a.com/a.text.
I need to get the text data only. My code so far:
// this.Hide();
// notifyIcon1.Visible = true;
WebClient Get= new WebClient();
Uri Line = new Uri("http://asap.eb2a.com/a.text");
AAA:
var text = Get.DownloadData(Line);
// var text = System.IO.File.ReadAllText(@"D:\b.txt");
//System.IO.File.Delete(@"D:\Not.text");
label1.Text = text.ToString();
while (text.ToString() == text.ToString())
try
{
notifyIcon1.Visible = true;
notifyIcon1.BalloonTipText = (Convert.ToString(text));
notifyIcon1.BalloonTipTitle = "We Notify You";
notifyIcon1.ShowBalloonTip(150);
BBB: var text1 = Get.DownloadData(Line);
//System.IO.File.ReadAllText(@"D:\b.txt");
while(text.ToString()==text1.ToString())
{
await Task.Delay(50);
goto BBB;
}
await Task.Delay(50);
goto AAA;
}
catch
{
MessageBox.Show("Error");
}
In order to make sure that the data is not the same I do some loops and I get this in Notify
I assume that your problem is the HTML text showed in the notify icon text. Probably the web site (asap.eb2a.com...) does not allow unknown agents, so it returns some message to inform you or the a.text
file contains HTML and JavaScript code. The first problem can be bypassed by adding a header to the WebClient object to mask your call as if it is coming from a web browser:
Get.Headers.Add ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
The loops created using the goto/label
construct can be replaced by a System.Timers.Timer class. This will also simplify your code:
100
millisecondsThe implementation looks like this:
WebClient Get = new WebClient();
// identify your self as a web browser
Get.Headers.Add ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
Uri uri = Uri("http://asap.eb2a.com/a.text");
string contentToShow = String.Empty;
// timer for the repetitive task called every nnn milliseconds
const long refreshIntervalInMilliseconds = 100;
System.Timers.Timer timer = new System.Timers.Timer(refreshIntervalInMilliseconds);
timer.Elapsed += (s, e) =>
{
Console.WriteLine(String.Format("{0} Fetching data from: {1}", DateTime.Now, uri));
try
{
var result = Get.DownloadString(uri);
if (result != contentToShow)
{
contentToShow = result;
notifyIcon1.Visible = true;
notifyIcon1.BalloonTipText = contentToShow;
notifyIcon1.BalloonTipTitle = "We Notify You";
notifyIcon1.ShowBalloonTip(150);
}
}
catch (Exception exception)
{
MessageBox.Show(string.Format("Error: {0}", exception.Message));
}
};
// start the timer
timer.Start();
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments