简体   繁体   中英

C# downloading page returns old page

I have problem, i wrote method to get current song on Czech radio. They do not have API so i had to get song from html via html agility.dll Problem is even though song title changes on page my method downloads old page, usually i have to wait like 20 seconds and have my app closed, then it works. I thought some cache problem, but i could not fix it. tried: DownloadString method did not refresh either.

public static string[] GetEV2Songs()
{
     List<string> songy = new List<string>();
     string urlAddress = "http://www.evropa2.cz/";
     string data = "";
     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);

     HttpWebResponse response = (HttpWebResponse)request.GetResponse();

     if (response.StatusCode == HttpStatusCode.OK)
     {
         Stream receiveStream = response.GetResponseStream();
         StreamReader readStream = null;

         if (response.CharacterSet == null)
             readStream = new StreamReader(receiveStream);
         else
             readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));

         data = readStream.ReadToEnd();

         response.Close();
         readStream.Close();
    }

    HtmlDocument doc = new HtmlDocument();
    doc.LoadHtml(data);
    string temp = "";

    foreach (var node in doc.DocumentNode.SelectNodes("//body//h2"))
    {
            if (node.InnerText.Contains("&ndash"))
            {
                temp = node.InnerText.Replace("&ndash;", "-");
                songy.Add(temp);

            }
     }

     return songy.ToArray();
 }

Sounds like being a caching problem. Try to replace the 4th line with something like that:

string urlAddress = "http://www.evropa2.cz/?_=" + System.Guid.NewGuid().ToString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM