我尝试使用webclient将单词'Banana'翻译成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));
}
我希望在“_stranslate”中看到
{“sentence”:[{“trans”:“Банан”,“orig”:“Banana @”,“translit”:“Banan @”,“src_translit”:“”}],“src”:“烯”, “server_time”:0}
但得到了这个
{“sentence”:[{“trans”:“вБОБО”,“orig”:“Banana @”,“translit”:“Banan @”,“src_translit”:“”}},“src”:“烯”, “server_time”:0}
有人可以帮助我吗?
答案 0 :(得分:13)
尝试检查响应标头,内容类型告诉您应使用的编码。
Content-Type => text/javascript; charset=KOI8-R
所以只需添加此行。
client.Encoding = Encoding.GetEncoding(20866);
或
client.Encoding = Encoding.GetEncoding("KOI8-R");
的文档中找到完整的编码列表
另一种方法是使用System.Net.Mime.ContentType
来获取字符集。
像这样:
byte[] data = client.DownloadData(url);
ContentType contentType = new System.Net.Mime.ContentType(client.ResponseHeaders[HttpResponseHeader.ContentType]);
string _stranslate = Encoding.GetEncoding(contentType.CharSet).GetString(data);
答案 1 :(得分:3)
在client.DownloadString()
:
client.Encoding = System.Text.Encoding.UTF8;
当您阅读字符串时,您的编码可能会搞砸。
使用this HTTP header viewer并输入您的网址,我在标题中看到以下内容:
Content-Type: text/javascript; charset=UTF-8
Content-Language: ru
基本上,您需要找出他们发回的编码并设置您的编码以匹配。
在调用DownloadString()
之前设置编码非常重要。
答案 2 :(得分:0)
恕我直言更好的解决方案:添加URI查询参数 的 OE = UTF-8 强> 并在任何地方使用UTF-8
var nameValueCollection = new NameValueCollection
{
{"client", "x"},
{"text", HttpUtility.UrlEncode(text)},
{"hl", "en"},
{"sl", fromLanguage},
{"tl", toLanguage},
{"ie", "UTF-8"},
{"oe", "UTF-8"}
};
string htmlResult;
using (var wc = new WebClient())
{
wc.Encoding = Encoding.UTF8;
wc.QueryString = nameValueCollection;
htmlResult = wc.DownloadString(GoogleAddress);
}