如何使用WebClient或HttpWebRequest或任何.NET客户端防止字符转义?

时间:2015-07-15 02:27:31

标签: system.net

我正在向http://translate.google.com/translate_tts?q=da%C3%B1o&tl=es-ES请求录音。当我使用System.Net.WebClient,System.Net.Http.HttpClient或WebRequest.Create时,请求将作为http://translate.google.com/translate_tts?q=daño&tl=es-ES发送。第二个网址有效,但结果不正确。

如何在路径中发送%C3%B1而非ñ的请求?

请注意,您无法使用引荐标题下载这些网址(例如,通过点击它们),因此如果您点击它们,它们将会失败并且结果可能会被缓存,因此复制'n'pasting网址也会失败。如果要测试URL,请将它们复制到剪贴板,然后添加到q参数以避免缓存结果。

在我尝试使用PUrify时添加repro(结果与没有相同):

            var parameters = this.Bind<SpeechRequest>();
            parameters.q = "daño";
            parameters.tl = "es-MX";

            var url = String.Format("http://translate.google.com/translate_tts?q={0}&tl={1}", 
                WebUtility.UrlEncode(parameters.q), WebUtility.UrlEncode(parameters.tl));

            var uri = new Uri(url);
            System.Console.WriteLine("Uri constructed from string: " + url);
            System.Console.WriteLine("Before Purify: ");
            ShowUriDetails(uri);
            uri.Purify();
            System.Console.WriteLine("After Purify:");
            ShowUriDetails(uri);

            var request = System.Net.WebRequest.Create(uri) as HttpWebRequest; 

输出:

    Uri constructed from string: http://translate.google.com/translate_tts?q=da%C3%B1o&tl=es-MX
    Before Purify:
            uri.ToString() - http://translate.google.com/translate_tts?q=daño&tl=es-MX
            uri.AbsoluteUri - http://translate.google.com/translate_tts?q=da%C3%B1o&tl=es-MX
            uri.Host - translate.google.com
            uri.Query - ?q=da%C3%B1o&tl=es-MX
            uri.PathAndQuery - /translate_tts?q=da%C3%B1o&tl=es-MX
            uri.AbsolutePath - /translate_tts
            uri.Fragment -
    After Purify:
            uri.ToString() - http://translate.google.com/translate_tts?q=daño&tl=es-MX
            uri.AbsoluteUri - http://translate.google.com/translate_tts?q=da%C3%B1o&tl=es-MX
            uri.Host - translate.google.com
            uri.Query - ?q=da%C3%B1o&tl=es-MX
            uri.PathAndQuery - /translate_tts?q=da%C3%B1o&tl=es-MX
            uri.AbsolutePath - /translate_tts
            uri.Fragment -

0 个答案:

没有答案