首页 > 编程 > .NET > 正文

ASP.NET技巧:远程抓取GOOGLE的自动翻译结果

2024-07-10 13:09:22
字体:
来源:转载
供稿:网友
注册会员,创建你的web开发资料库, 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,"&nbsp;","")
 6                     return s_str
 7            end if
 8        end function
 9       
10        function china_to_english()function china_to_english(texts,languages)
11            dim payload as string = "hl=zh-cn&ie=utf8&text="& texts &"&langpair="& languages &""
12            dim str_return as string 
13            dim req as webrequest = webrequest.create("http://translate.google.com/translate_t") ' 开始取连接.
14            req.credentials = credentialcache.defaultcredentials       '取得默认
15            req.method = "post"                                        '以post方式发送,这里默认是以get方式发送
16            req.contenttype = "application/x-www-form-urlencoded"      'post方式需在传送这个编码,如果上传文件,则修为multipart/form-data
17            req.timeout=10000                                          '连接超时定时
18            req.contentlength = payload.length                         '头部长度
19            dim encoding as encoding = encoding.getencoding("utf-8")   '转换成流,大部网站一般转换成utf-8就可以了,注意是大写的编码
20            dim bytes as byte() = encoding.getbytes(payload)           '转换成流
21            req.contentlength = bytes.length                           '传送流的长度
22            dim newstream as stream = req.getrequeststream()           '转换写入
23            newstream.write(bytes, 0, bytes.length)                    '写入传送流
24            newstream.close()                                          '关闭
25            '上面发送完成,下面取得服务器返回
26            dim res as httpwebresponse = ctype(req.getresponse(), httpwebresponse)  ' 传递返回标识
27            if res.statusdescription="ok" then  ' 返回取得状态.
28               current.response.write("暂时无法连接到网站,请换用另一个程序")
29               current.response.end()
30            end if
31            dim datastream as stream = res.getresponsestream()         ' 返回给指针
32            dim reader as new streamreader(datastream,encoding.getencoding("gb2312")) ' 读
33            dim responsefromserver as string = reader.readtoend()      ' 读取所有
34           
35            str_return=responsefromserver                              '赋值回传
36           
37            reader.close()                                             '接下来三个关闭
38            datastream.close()
39            res.close()
40
41            dim ss as string = str_return
42            ss = regex.replace(ss,"(?i:(.+)(/<div)(.+)(/>)(.+)(/<//div/>)(.+))","$5") '提取我们所要的译文
43            ss = removehtml(ss)                                       '删除html
44            ss = ss.substring(3)                                      '删除翻译二字
45            return ss                                                 '函数返回
46    end function
47       
48    在调用china_to_english(texts,languages)需要传两个参数第一个为要译的文字,第二个是要进行相对译的语种代码.
   例:中译英,其第二个参数为:zh-cn|en
   我导入的命名空间如下:
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
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表