private string WrGenericTranslate(string CrawlerCd, string TrlCode, string InStr, string dbConnectionString, string dbPassword) { HttpWebRequest request; string posttext; DataTable dt = null; string rtn = string.Empty; using (WebAccessBase dac = GetWebAccess()) { dt = dac.WrGetCtCrawler(CrawlerCd); } try { if (CrawlerCd == "GA") { // Get: posttext = dt.Rows[0]["PreText"].ToString() + TrlCode + dt.Rows[0]["PostText"].ToString() + HttpUtility.UrlEncode(InStr.Replace("\\", "/")); request = (HttpWebRequest)WebRequest.Create(new Uri(dt.Rows[0]["CrawlerURL"].ToString() + posttext)); } else if (CrawlerCd == "GB") { // Get google translation paid api v2: string apiKey = base.DecryptString(Config.GoogleAPIKey); string[] fromTo = TrlCode.Split(new char[] { '|' }); posttext = dt.Rows[0]["PreText"].ToString() + apiKey + string.Format("&source={0}&target={1}", fromTo[0], fromTo[1]) + dt.Rows[0]["PostText"].ToString() + HttpUtility.UrlEncode(InStr.Replace("\\", "/")); request = (HttpWebRequest)WebRequest.Create(new Uri(dt.Rows[0]["CrawlerURL"].ToString() + posttext)); } else { // Post: request = (HttpWebRequest)WebRequest.Create(new Uri(dt.Rows[0]["CrawlerURL"].ToString())); posttext = dt.Rows[0]["PreText"].ToString() + TrlCode + dt.Rows[0]["PostText"].ToString() + HttpUtility.UrlEncode(InStr.Replace("\\", "/")); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = posttext.Length; request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"; Stream writeStream = request.GetRequestStream(); UTF8Encoding encoding = new UTF8Encoding(); byte[] bytes = encoding.GetBytes(posttext); writeStream.Write(bytes, 0, bytes.Length); writeStream.Close(); } request.Referer = dt.Rows[0]["CrawlerREF"].ToString(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader readStream = new StreamReader(response.GetResponseStream(), Encoding.UTF8); string page = readStream.ReadToEnd(); Regex reg = new Regex(dt.Rows[0]["ResultRegex"].ToString(), RegexOptions.IgnoreCase | RegexOptions.Multiline); MatchCollection matches = reg.Matches(page); if (matches.Count == 1 && matches[0].Groups.Count == 2 && matches[0].Groups[1].Value != string.Empty) { rtn = matches[0].Groups[1].Value; } } catch (Exception err) { ApplicationAssert.CheckCondition(false, "WrGenericTranslate", dt.Rows[0]["CrawlerURL"].ToString() + ": " + err.Message, "Translation engine cannot handle \"" + InStr + "\". Please investigate and try again."); } if (CrawlerCd == "GA" || CrawlerCd == "GB") { // google translate API returns JSONEncode(htmlencoded(string)) and we need to reverse it var json_encoded = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize <System.Collections.Generic.Dictionary <string, string> >(string.Format("{{\"translatedText\":\"{0}\"}}", rtn)); rtn = System.Web.HttpUtility.HtmlDecode(json_encoded["translatedText"]); } return(rtn); }