Asp.net Google's Translate Tool translation API

Source: Internet
Author: User

In this article, I will use C # To write a small Program , Translation:
The idea is as follows:
1: Send post (or get)
2: Get the post (or get) Response
3: The regular expression matches the value we want.
Post (or get) function: CopyCode The Code is as follows: 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;
}

Here are some of my firstArticle.
Then, the regular expression matches the following functions:Copy codeThe Code is as follows: 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 ();
}

This number returns a matching Value Based on a regular expression.
Directly go to the main body:Copy codeThe Code is as follows: public static void main (string [] ARGs ){
String mess = "we ";
Console. writeline (httputility. urlencode ("we "));
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] *?) (</Div>) ", 2 );
Console. writeline (MESS );
}

Note that
Httputility. urlencode (mess, system. Text. unicodeencoding. getencoding ("gb2312 "))
This sentence cannot recognize the character encoding of urlencode, which must be specified here.
OK, then CSC. Compile and download it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.