Search code examples
c#winformshttpclient

How to read that from from a website and do something


I want to read the data from a website like if on the website says SAY_HELLO to display a message box which says hello world or if it says SAY_HELLOME to display a message box that says Hello me

 WebClient client = new WebClient();
            Stream str = client.OpenRead("http://localhost/api/maintenance.php");
            StreamReader reader = new StreamReader(str);
            String content = reader.ReadToEnd();

Solution

  • string webURL = "http://localhost/api/maintenance.php";
    WebClient wc = new WebClient();
    wc.Headers.Add("user-agent", "Only a Header!");
    byte[] rawByteArray = wc.DownloadData(webURL);
    string webContent = Encoding.UTF8.GetString(rawByteArray);
    
    if (webContent.ToUpper().Contains("SAY_HELLO"))
         MessageBox.Show("hello world");
    else if (webContent.ToUpper().Contains("SAY_HELLOME"))
         MessageBox.Show("hello me");