当前位置: 首页>>代码示例>>C#>>正文


C# WebClient.DownloadStringUsingResponseEncoding方法代码示例

本文整理汇总了C#中System.Net.WebClient.DownloadStringUsingResponseEncoding方法的典型用法代码示例。如果您正苦于以下问题:C# WebClient.DownloadStringUsingResponseEncoding方法的具体用法?C# WebClient.DownloadStringUsingResponseEncoding怎么用?C# WebClient.DownloadStringUsingResponseEncoding使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Net.WebClient的用法示例。


在下文中一共展示了WebClient.DownloadStringUsingResponseEncoding方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TranslateText

 /// <summary>
 /// Translate Text using Google Translate
 /// </summary>
 /// <param name="input">The string you want translated</param>
 /// <param name="languagePair">2 letter Language Pair, delimited by "|". 
 /// e.g. "en|da" language pair means to translate from English to Danish</param>
 /// <param name="encoding">The encoding.</param>
 /// <returns>Translated to String</returns>
 public static string TranslateText(string input, string languagePair)
 {
     string url = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", System.Web.HttpUtility.UrlEncode(input), languagePair);
     WebClient webClient = new WebClient();
     webClient.Encoding = System.Text.Encoding.UTF8;
     string result = webClient.DownloadStringUsingResponseEncoding(url);
     result = result.Substring(result.IndexOf("<span title=\"") + "<span title=\"".Length);
     result = result.Substring(result.IndexOf(">") + 1);
     result = result.Substring(0, result.IndexOf("</span>"));
     return System.Web.HttpUtility.HtmlDecode(result.Trim());
 }
开发者ID:GregoryMorse,项目名称:ResxTranslator,代码行数:19,代码来源:Translator.cs

示例2: TranslateText

        /// <summary>
        /// Translate Text using Google Translate
        /// </summary>
        /// <param name="input">The string you want translated</param>
        /// <param name="languagePair">2 letter Language Pair, delimited by "|". 
        /// e.g. "en|da" language pair means to translate from English to Danish</param>
        /// <param name="encoding">The encoding.</param>
        /// <returns>Translated to String</returns>
        public static string TranslateText(string input, string languagePair)
        {
            //string url = String.Format("http://www.google.hr/translate_t?hl=hr&ie=UTF8&text={0}&langpair={1}", Uri.EscapeUriString(input).Replace("#", "%23"), languagePair);
            string url = String.Format("http://translate.google.com/?hl=en&ie=UTF8&text={0}&langpair={1}", Uri.EscapeUriString(input).Replace("#", "%23"), languagePair);

            var webClient = new WebClient { Encoding = Encoding.UTF8 };

            // MUST add a known browser user agent or else response encoding doen't return UTF-8 (WTF Google?)
            webClient.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/5.0");
            webClient.Headers.Add(HttpRequestHeader.AcceptCharset, "UTF-8");

            string result = webClient.DownloadStringUsingResponseEncoding(url);

            result = result.Substring(result.IndexOf("<span title=\"") + "<span title=\"".Length);
            result = result.Substring(result.IndexOf(">") + 1);
            result = result.Substring(0, result.IndexOf("</span>"));

            return result.Trim();
        }
开发者ID:Gevil,项目名称:Projects,代码行数:27,代码来源:GoogleTranslate.cs


注:本文中的System.Net.WebClient.DownloadStringUsingResponseEncoding方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。