我尝试使用Web客户端,以词“香蕉”翻译成俄文
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”看
{ “句子”:[{ “反式”: “Банан”, “原稿”: “香蕉@”, “TRANSLIT”: “巴南@”, “src_translit”: “”}], “SRC”: “恩”, “server_time”:0}
但得到这个
{ “句子”:[{ “反式”: “вБОБО”, “原稿”: “香蕉@”, “TRANSLIT”: “巴南@”, “src_translit”: “”}], “SRC”: “恩”, “server_time”:0}
有人能帮我吗?
请检查响应报头,内容类型告诉你什么编码,你应该使用。
Content-Type => text/javascript; charset=KOI8-R
因此,只要加入这一行。
client.Encoding = Encoding.GetEncoding(20866);
要么
client.Encoding = Encoding.GetEncoding("KOI8-R");
对于编码的完整列表可在该文档中找到Encoding类
另一种方法是只使用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);
您之前加入这个client.DownloadString()
client.Encoding = System.Text.Encoding.UTF8;
你的编码,当你阅读的字符串可能得到搞砸。
使用此HTTP标头查看器 ,并把你的网址,我看到标题的情况如下:
Content-Type: text/javascript; charset=UTF-8
Content-Language: ru
基本上,你需要找出他们发回什么编码,并设置您的编码相匹配。
打电话之前设置编码是非常重要的DownloadString()
恕我直言更好的解决办法:增加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);
}