以下代码实现抓取google翻译结果,能否改写为asp代码,本人不会.net,期待高人啊.........跪求
以下代码实现抓取google翻译结果,能否改写为asp代码,本人不会.net,期待高人啊.........跪求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
Public Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Response.Write(china_to_english("母牛", "zh-Cn|en"))
End Sub
Public Function RemoveHTML(ByVal strhtml As String) As String
If strhtml <> "" Then
Dim s_str As String
s_str = Regex.Replace(strhtml, "<[^>]+>", "")
s_str = Replace(s_str, " ", "")
Return s_str
End If
Return String.Empty
End Function
Public Function china_to_english(ByVal texts As String, ByVal languages As String) As String
Dim payload As String = "hl=zh-CN&ie=UTF8&text=" & texts & "&langpair=" & languages & ""
Dim str_return As String
Dim req As WebRequest = WebRequest.Create("http://translate.google.com/translate_t") ' 开始取连接.
req.Credentials = CredentialCache.DefaultCredentials '取得默认
req.Method = "POST" '以POST方式发送,这里默认是以GET方式发送
req.ContentType = "application/x-www-form-urlencoded" 'POST方式需在传送这个编码,如果上传文件,则修为multipart/form-data
req.Timeout = 10000 '连接超时定时
req.ContentLength = payload.Length '头部长度
Dim encoding As Encoding = encoding.GetEncoding("UTF-8") '转换成流,大部网站一般转换成UTF-8就可以了,注意是大写的编码
Dim bytes As Byte() = encoding.GetBytes(payload) '转换成流
req.ContentLength = bytes.Length '传送流的长度
Dim newStream As Stream = req.GetRequestStream() '转换写入
newStream.Write(bytes, 0, bytes.Length) '写入传送流
newStream.Close() '关闭
'上面发送完成,下面取得服务器返回
Dim res As HttpWebResponse = CType(req.GetResponse(), HttpWebResponse) ' 传递返回标识
If res.StatusDescription = "ok" Then ' 返回取得状态.
Current.Response.Write("暂时无法连接到网站,请换用另一个程序www.133jz.com")
Current.Response.End()
End If
Dim dataStream As Stream = res.GetResponseStream() ' 返回给指针
Dim reader As New StreamReader(dataStream, encoding.GetEncoding("gb2312")) ' 读
Dim responseFromServer As String = reader.ReadToEnd() ' 读取所有
str_return = responseFromServer '赋值回传
reader.Close() '接下来三个关闭
dataStream.Close()
res.Close()
Dim ss As String = str_return
ss = Regex.Replace(ss, "(?i:(.+)(\<div)(.+)(\>)(.+)(\<\/div\>)(.+))", "$5") '提取我们所要的译文
ss = RemoveHTML(ss) '删除HTML
ss = ss.Substring(3) '删除翻译二字
Return ss '函数返回
End Function
End Class
补充:.NET技术 , ASP.NET