asp.net Google的translate工具翻譯 API
思路是這樣的:
1:發(fā)送POST(或者GET)
2:獲取POST(或者GET)的響應(yīng)
3:正則匹配我們想要的值。
發(fā)生POST(或者GET)的函數(shù):
public static string GetGetRequest(string urlP,string encode){
if(null==urlP) return null;
string StrRetP = null;
Stream dataStream = null;
try{
HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create(urlP);
myHttpWebRequest.Timeout = 10000; // 10 secs
HttpWebResponse Objresponse =(HttpWebResponse)myHttpWebRequest.GetResponse();
//Encoding enc = Encoding.GetEncoding(1252); // Windows default Code Page
if(Objresponse.StatusDescription == "OK"){//HttpStatusCode.OK
dataStream = Objresponse.GetResponseStream ();
Encoding objE = String.IsNullOrEmpty(encode)?Encoding.GetEncoding(0):Encoding.GetEncoding(encode);
StreamReader r = new StreamReader(dataStream,objE);
StrRetP= r.ReadToEnd();
}
}catch(Exception e){
StrRetP =e.Message;
}finally{
if(null!=dataStream) dataStream.Close();
}
return StrRetP;
}
這個(gè)我在前面的一些文章中有所介紹。
然后正則匹配的函數(shù):
public static string GetMatchString(string text,string pattern,int point){
if(String.IsNullOrEmpty(text)||String.IsNullOrEmpty(pattern))return String.Empty;
Regex rx = new Regex(pattern,RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Multiline);
Match match = rx.Match(text);
string word="";
if (match.Success) word = match.Groups[point].Value;
return word.Trim();
}
這個(gè)數(shù)根據(jù)一個(gè)正則表達(dá)數(shù),返回匹配的值。
直接進(jìn)入Main主體:
public static void Main(string[] args){
string mess ="我們";
Console.WriteLine(HttpUtility.UrlEncode("我們"));
mess = GetGetRequest("http://translate.google.com/translate_t?langpair="+HttpUtility.UrlEncode("zh-CN|en")+ "&text="+HttpUtility.UrlEncode(mess,System.Text.UnicodeEncoding.GetEncoding( "Gb2312")),"utf-8");
//Console.WriteLine(mess);
mess = GetMatchString(mess,@"(<div id=result_box dir=""ltr"">)([?:\s\S]*?)(</div>)",2);
Console.WriteLine(mess);
}
注意的是
HttpUtility.UrlEncode(mess,System.Text.UnicodeEncoding.GetEncoding( "Gb2312"))
這句,無(wú)法識(shí)別UrlEncode的字符編碼,這里需要指明。
OK,然后csc了,編譯一下,下載一下吧。
- asp.net下URL處理兩個(gè)小工具方法
- Asp.Net權(quán)限管理系統(tǒng) 專用代碼生成工具(DDBuildTools) 1.1.0 下載
- asp.net 半角全角轉(zhuǎn)化工具
- asp.net 實(shí)現(xiàn)防迅雷等下載工具盜鏈
- asp.net(c#)做一個(gè)網(wǎng)頁(yè)數(shù)據(jù)采集工具
- IE 性能分析工具(asp.net環(huán)境)
- asp.net 專業(yè)源代碼打包工具
- asp.net防止刷新時(shí)重復(fù)提交(可禁用工具條刷新按鈕)
- ASP.NET開(kāi)發(fā)中經(jīng)常用到10款工具軟件介紹
相關(guān)文章
ASP.NET MVC過(guò)濾器執(zhí)行順序介紹
這篇文章介紹了ASP.NET MVC過(guò)濾器的執(zhí)行順序,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03asp.net(c#)網(wǎng)頁(yè)跳轉(zhuǎn)七種方法小結(jié)
在asp.net下,經(jīng)常需要頁(yè)面的跳轉(zhuǎn),下面是具體的幾種方法。跳轉(zhuǎn)頁(yè)面是大部編輯語(yǔ)言中都會(huì)有的,正面我們來(lái)分別介紹一下關(guān)于.net中response.redirect sever.execute server.transfer 三種頁(yè)面跳轉(zhuǎn)的方法2009-11-11gridview行索引獲取方法及實(shí)現(xiàn)代碼
GridView行索引的獲取有利于對(duì)GridView行數(shù)據(jù)進(jìn)行操作(刪、改)等等,接下來(lái)介紹獲取方法,感興趣的朋友可以了解下,閱讀本文希望對(duì)你有幫助2013-01-01dataGrid 多維表頭、表頭跨行跨列設(shè)計(jì)及綁定數(shù)據(jù)
dataGrid 其實(shí)就是一個(gè)html table,本文將介紹dataGrid 多維表頭,表頭跨行跨列設(shè)計(jì)方法需要了解的朋友可以參考下2012-12-12基于ASP.NET實(shí)現(xiàn)日期轉(zhuǎn)為大寫(xiě)的漢字
在寫(xiě)代碼時(shí)遇到一個(gè)需要將日期轉(zhuǎn)換為中文大寫(xiě)日期的問(wèn)題,網(wǎng)上找了找,示例不是很多,只有javascript代碼的,下面整理下在ASP.NET中怎么實(shí)現(xiàn)。2016-08-08Asp.net core利用dynamic簡(jiǎn)化數(shù)據(jù)庫(kù)訪問(wèn)
這篇文章介紹了Asp.net core利用dynamic簡(jiǎn)化數(shù)據(jù)庫(kù)訪問(wèn)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07用.NET 2.0壓縮/解壓功能處理大型數(shù)據(jù)
用.NET 2.0壓縮/解壓功能處理大型數(shù)據(jù)...2006-09-09ASP.NET設(shè)計(jì)網(wǎng)絡(luò)硬盤(pán)之上傳文件實(shí)現(xiàn)代碼
用戶最終是要和文件打交道的,文件夾僅僅是用來(lái)方便管理的。文件的上傳和下載也就成為“網(wǎng)絡(luò)硬盤(pán)”功能設(shè)計(jì)中的重要一環(huán)2012-10-10