嘿:)我真的很努力让WebClient回复我UTF-8。但是当sub应该返回类似Ä
之类的东西时,我认为它更像E
左右。
尝试了很多变通方法,但它不会起作用。
private string translate(string input, string languagePair)
{
string url = String.Format("https://translate.google.com/?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair);
WebClient wc = new WebClient();
wc.Headers.Add(HttpRequestHeader.AcceptCharset, "UTF-8");
wc.Encoding = Encoding.UTF8;
var data = wc.DownloadData(url);
var result = Encoding.UTF8.GetString(data);
//string result = wc.DownloadString(url);
int start = result.IndexOf("result_box");
string sub = result.Substring(start);
sub = sub.Substring(0, sub.IndexOf("</span>"));
start = sub.LastIndexOf(">");
sub = sub.Substring(start + 1);
return sub;
}
答案 0 :(得分:4)
谷歌只是忽略AcceptCharset
标题中发送的编码并返回ISO-8859-1
中的回复,正如您从缩短的回复中看到的那样:
HTTP/1.1 200 OK
Content-Type: text/html; charset=ISO-8859-1
Content-Language: en
Content-Length: 64202
<!DOCTYPE html><html><head><meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
因此,当您使用UTF-8编码解码响应时,您将获得无效字符。如果您想让它快速运行,我发现当User-Agent
标头添加到请求时,Google会以UTF-8返回响应,您可以不修改其余代码:
private static string translate(string input, string languagePair)
{
string url = String.Format("https://translate.google.com/?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair);
WebClient wc = new WebClient();
wc.Headers.Add(HttpRequestHeader.AcceptCharset, "utf-8");
wc.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/5.0 (Windows NT 10.0; …) Gecko/20100101 Firefox/55.0");
wc.Encoding = Encoding.UTF8;
string result = wc.DownloadString(url);
int start = result.IndexOf("result_box");
string sub = result.Substring(start);
sub = sub.Substring(0, sub.IndexOf("</span>"));
start = sub.LastIndexOf(">");
sub = sub.Substring(start + 1);
return sub;
}
更好的解决方案是检测响应中使用的编码并将其用于解码。 WebClient
内置了此检测功能,因此您可以使用here描述的解决方案或使用HttpClient
代替,这会自动为您执行此操作:
private static async Task<string> translate(string input, string languagePair)
{
string url = String.Format("https://translate.google.com/?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair);
using (var hc = new HttpClient())
{
var result = await hc.GetStringAsync(url).ConfigureAwait(false);
int start = result.IndexOf("result_box");
string sub = result.Substring(start);
sub = sub.Substring(0, sub.IndexOf("</span>"));
start = sub.LastIndexOf(">");
sub = sub.Substring(start + 1);
return sub;
}
}
另请注意,Google有Translation API,这可能更适合使用而不是解析HTML页面的翻译。