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.