简体   繁体   English

C#WebClient问题,使用带有URL编码西里尔字符的URL

[英]c# WebClient problem using a URL with URL-encoded cyrillic chars

I'm tryinig to load a file from a web server with a request URL that contains a parameter with cyrillic chars. 我正在tryinig从Web服务器加载文件,该文件的请求URL包含西里尔字符。 But I'm not getting this to work in c#, even if I URL-Encode the param. 但是,即使我对参数进行URL编码,我也无法在c#中使用它。

When I open the page in IE with 当我在IE中打开页面时
http://translate.google.com/translate_tts?tl=ru&q =ЗДРАВСТВУЙТЕ http://translate.google.com/translate_tts?tl=ru&q =ЗДРАВСТВУЙТЕ
the server does not respond. 服务器没有响应。

Using the URL-encoded version 使用网址编码版本
http://translate.google.com/translate_tts?tl=ru&q=%d0%97%d0%94%d0%a0%d0%90%d0%92%d0%a1%d0%a2%d0%92%d0%a3%d0%99%d0%a2%d0%95 http://translate.google.com/translate_tts?tl=ru&q=%d0%97%d0%94%d0%a0%d0%90%d0%92%d0%a1%d0%a2%d0%92%d0 %a3%d0%99%d0%a2%d0%95
the server responds as expected. 服务器响应预期。

Now my problem: 现在我的问题是:
I want to download the MP3 from C# ... 我想从C#下载MP3 ...

var url = string.Format("http://translate.google.com/translate_tts?tl=ru&q={0}", 
          Server.UrlEncode("ЗДРАВСТВУЙТЕ"));
System.Net.WebClient client = new WebClient(); 
var res = client.DownloadData(url);

And this does NOT work with cyrillic chars. 这不适用于西里尔字符。 I always get a zero-byte answer, like the first, non-encoded request. 我总是得到一个零字节的答案,就像第一个未编码的请求一样。 When I send "normal" chars, the code above works fine. 当我发送“普通”字符时,上面的代码可以正常工作。

So ... I'm doing something wrong. 所以...我做错了。 Any hints? 有什么提示吗? Tipps? 小费? Solutions? 解决方案?

Thanks 谢谢
Michael 麦可

You have to set the user-agent for the WebClient - this works: 您必须为WebClient设置用户代理-这有效:

string url = "http://translate.google.com/translate_tts?tl=ru&q=ЗДРАВСТВУЙТЕ";
WebClient client = new WebClient();
client.Headers.Add("user-agent", 
                   "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
var res = client.DownloadData(url);

From the msdn documentation : msdn文档

A WebClient instance does not send optional HTTP headers by default. 默认情况下,WebClient实例不发送可选的HTTP标头。 If your request requires an optional header, you must add the header to the Headers collection. 如果您的请求需要可选的标题,则必须将标题添加到Headers集合。 For example, to retain queries in the response, you must add a user-agent header. 例如,要保留响应中的查询,必须添加用户代理标头。 Also, servers may return 500 (Internal Server Error) if the user agent header is missing. 另外,如果缺少用户代理标头,则服务器可能返回500(内部服务器错误)。

Try to add 尝试添加

client.Encoding = System.Text.Encoding.UTF8;

I don't use user-agent header but for me it works: 我不使用用户代理标头,但对我来说它起作用:

WebClient client = new WebClient();
client.Encoding = System.Text.Encoding.UTF8;
string response = client.DownloadString(_url);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM