I try use webclient to translate word 'Banana' into rus
private void button1_Click(object sender, EventArgs e)
{
Navigate("http://translate.google.ru/translate_a/t?client=x&text=Banana&hl=en&sl=en&tl=ru");
}
private void Navigate(String address)
{
WebClient client = new WebClient();
client.Proxy = WebRequest.DefaultWebProxy;
client.Credentials = new NetworkCredential("user", "password", "domain");
client.Proxy.Credentials = new NetworkCredential("user", "password", "domain");
string _stranslate = client.DownloadString(new Uri(address));
}
And I want to see in "_stranslate "
{"sentences":[{"trans":"Банан","orig":"Banana@","translit":"Banan @","src_translit":""}],"src":"en","server_time":0}
but got this
{"sentences":[{"trans":"вБОБО","orig":"Banana@","translit":"Banan @","src_translit":""}],"src":"en","server_time":0}
Can some one help me?
Add this before your
client.DownloadString()
:Your encoding is likely getting messed up when you read the string.
Using this HTTP header viewer and putting in your URL, I see the following in the headers:
Basically, you need to find out what encoding they are sending back and set your encoding to match.
It is very important to set the encoding before you call
DownloadString()
.IMHO better solution: add URI query parameter oe=UTF-8 and use UTF-8 everywhere
Try checking the response headers, the content types tells you what encoding you should use.
Content-Type => text/javascript; charset=KOI8-R
So just add this line.
or
A complete list for encodings can be found in the documentation for the Encoding Class
Another way would be to just use
System.Net.Mime.ContentType
to get the charset. Like this: