2

I'm using this link: https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=en&dt=t&q=myTextHere

When I feed it Japanese characters such as, テスト中..., the DownloadString method returns strange characters such as this: ム† ã,¹ãƒä¸ ...

The correct string should be "Under Test..."

You can see for yourself by clicking the link on your browser: https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=en&dt=t&q=テスト中

I've tried multiple things such as setting the client's encoding to UTF-8 and using HttpUtility.UrlEncode(myText) but I could not get what my browser returns. Swapping DownloadString for DownloadFile as txt returns the same incorrect text. How do I get the same results as the browser?

Here's a small code snippet that resembles my environment:

String s = "テスト中";
Console.WriteLine("src="+s);
WebClient client = new WebClient();
client.Encoding = Encoding.UTF8;
string downloadString = @client.DownloadString("https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=en&dt=t&q=" + HttpUtility.UrlEncode(s));
Console.WriteLine("data:{\n"+downloadString+"\n}");
4

1 回答 1

2

我不知道为什么 Google Translate API 返回乱码。格式错误的 WebClient 响应包含“fr”而不是“ja”,这表明 API 将您的文本误解为法语 (!) 而不是日语。或者其他的东西。

无论如何,经过一些实验后,我发现如果您设置 User-Agent 标头,API 会正常运行:

WebClient client = new WebClient();
client.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)");
client.Encoding = Encoding.UTF8;
string downloadString = client.DownloadString("https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=en&dt=t&q=" + HttpUtility.UrlEncode(s));
// Result: [[["Under test","テスト中",null,null,3]],null,"ja",...]
于 2018-05-14T02:58:21.047 回答