Asp. NET tip: Remotely crawl Google's automated translation results

Source: Internet
Author: User
Tags net
Asp.net|google| Technique 1 Function removehtml () function removehtml (strhtml)
2 if strhtml<> "" Then
3 Dim s_str As String
4 S_str=regex.replace (strhtml, "<[^>]+>", "")
5 S_str=replace (S_str, "", "")
6 return S_str
7 End If
8 End Function
9
Ten function China_to_english () function china_to_english (texts,languages)
One Dim payload as String = "hl=zh-cn&ie=utf8&text=" & Texts & "&langpair=" & Languages &am p; ""
Dim Str_return As String
Dim req as WebRequest = WebRequest.Create ("http://translate.google.com/translate_t") ' begins to fetch the connection.
Req. Credentials = CredentialCache.DefaultCredentials ' Get default
Req. Method = "Post" is sent as post, where the default is to send by get
Req. ContentType = "Application/x-www-form-urlencoded" POST method is required to transmit this encoding, if uploaded files, then fix the Multipart/form-data
req.timeout=10000 ' Connection Timeout timed
Req. ContentLength = payload. Length ' head lengths
Dim encoding as Encoding = encoding.getencoding ("UTF-8") ' Convert to stream, large Web site generally converted to UTF-8 on it, note that uppercase code
Dim bytes as Byte () = encoding.getbytes (payload) ' Convert to stream
Req. contentlength = bytes. Length of the transfer flow
Dim newstream as Stream = req. GetRequestStream () ' Convert write
Newstream.write (bytes, 0, bytes.) Length) ' writes to the transport stream
Newstream.close () ' Off
25 ' above send complete, below get server return
Dim res as HttpWebResponse = CType (req. GetResponse (), HttpWebResponse) ' Pass return identification
If Res. statusdescription= "OK" Then ' returns to the obtained state.
Current.response.write ("temporarily unable to connect to the Web, please replace another program")
Current.response.end ()
End If
Dim DataStream as Stream = Res. GetResponseStream () ' Return to pointer
Dim Reader as New StreamReader (datastream,encoding.getencoding ("gb2312")) ' Read
Dim responsefromserver as String = reader. ReadToEnd () ' reads all
34
Str_return=responsefromserver ' assigned value return
36
Panax Notoginseng Reader. Close () ' Next three closes
Datastream.close ()
Res. Close ()
40
A Dim ss As String = Str_return
The SS = Regex.Replace (ss, "(? I: (. +) (\<div) (. +) (\>) (. +) (\<\/div\>) (. +)", "$") ' Extract the translations we want
The SS = removehtml (ss) ' Delete HTML
SS = ss.substring (3) ' Delete translation two words
return SS ' function returns
The End Function
47
48 in the call China_to_english (texts,languages) need to pass two parameters first for the text to be translated, the second is to do relative translation of the language code.
For example, the second parameter of Chinese-English translation is: Zh-cn|en
The namespaces I import are as follows:
Imports System
Imports system.web
Imports System.IO
Imports Microsoft.VisualBasic
Imports System.Web.HttpContext
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Text
Imports System.Text.RegularExpressions
Imports System.Net

Related Article

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.