1

Im trying to search a Hebrew word in a website using c# but i cant figure it out. this is my current state code that im trying to work with:

var client = new WebClient();
        Encoding encoding = Encoding.GetEncoding(1255);
        var text = client.DownloadString("http://shchakim.iscool.co.il/default.aspx");

        if (text.Contains("ביטול"))
        {
            MessageBox.Show("idk");
        }

thanks for any help :)

Itay
  • 17
  • 2
  • Check [This](https://stackoverflow.com/questions/4758283/reading-data-from-a-website-using-c-sharp) – NaDeR Star Jan 27 '19 at 21:28
  • When you stepped through the code in the debugger what was in `text`, i.e. are you having trouble retrieving the data or searching through it? – HABO Jan 27 '19 at 22:24

1 Answers1

2

The problem seems to be that WebClient is not using the right encoding when converting the response into a string, you must set the WebClient.Encoding property to the expected encoding from the server for this conversion to happen correctly.

I inspected the response from the server and it's encoded using utf-8, the updated code below reflects this change:

using (var client = new WebClient())
{
    client.Encoding = System.Text.Encoding.UTF8;

    var text = client.DownloadString("http://shchakim.iscool.co.il/default.aspx");

    // The response from the server doesn't contains the word ביטול, therefore, for demo purposes I changed it for שוחרות which is present in the response.
    if (text.Contains("שוחרות"))
    {
        MessageBox.Show("idk");
    }
}

Here you can find more information about the WebClient.Encoding property: https://learn.microsoft.com/en-us/dotnet/api/system.net.webclient.encoding?view=netframework-4.7.2

Hope this helps.

yv989c
  • 1,453
  • 1
  • 14
  • 20
  • I ran both code samples through JDoodle and got errors. Is the code meant to go within a method? – K Man Jan 28 '19 at 01:00
  • Yes, the code must be inside a method, like Main. I tried it myself at jdoodle and I got a "System.Net.WebException: Error: NameResolutionFailure", which probably means they don't allow web request from inside their service (which for security reasons makes sense). If you try it locally in a Windows Forms application it will work. You also need the "using System.Net;" at the top of your .cs file. – yv989c Jan 28 '19 at 01:09