本文整理汇总了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());
}
示例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();
}