无法从谷歌翻译获得中文

时间:2014-06-11 08:26:45

标签: c# encoding google-translate

这是我的代码,我尝试了System.Text.Encoding中的所有编码,但没有让它工作,但它正在为bangali工作

string input = "i eat rice";
string languagePair = "en|zh-CN";

string url = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair);
WebClient webClient = new WebClient();
webClient.Encoding = System.Text.Encoding.UTF8;
webClient.Proxy = WebRequest.DefaultWebProxy;
webClient.Credentials = new NetworkCredential("myname", "mypassword", "mydomain");
webClient.Proxy.Credentials = new NetworkCredential("myname", "mypassword", "mydomain");


String result = webClient.DownloadString(url);
result = result.Substring(result.IndexOf("<span title=\"") + "<span title=\"".Length);
result = result.Substring(result.IndexOf(">") + 1);
result = result.Substring(0, result.IndexOf("</span>"));

result = WebUtility.HtmlDecode(result);

我正在尝试不同的组合,带来原始的中文字符,从这里开始

 byte[] dumpToBytes = GetBytes(result);


        // Convert a string to utf-8 bytes.
 byte[] utf8Bytes = System.Text.Encoding.UTF8.GetBytes(result);

        // Convert utf-8 bytes to a string.
 string s_unicode2 = System.Text.Encoding.Unicode.GetString(utf8Bytes);

 Console.WriteLine(s_unicode2);

 byte [] arr= System.Text.UnicodeEncoding.Convert(System.Text.Encoding.UTF8, System.Text.Encoding.Unicode,dumpToBytes);
 result = GetString(arr);
 Console.WriteLine(result);

 Console.ReadLine();

无论我做什么,我得到的都是特殊的角色,任何帮助都将被赞赏...... :(

0 个答案:

没有答案