国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

ASP使用Google的translate API

2019-11-17 04:12:09
字體:
來源:轉載
供稿:網友

上篇文字php使用Google的translate API寫了以后,突然想做個asp的類,以致asp用戶使用。遂寫起,發現asp忘了不少。如主題吧,思路和C#的這篇是完全一樣的,我只是把它兌成ASP的實現方式而已。

  直接看代碼:
 
Class GoogleTranslator

    sub Class_Initialize()
        RURI="    End Sub

    PRivate Opt_    '   
    Property Get Opt
        Opt=Opt_
    End Property
    Property Let Opt(Opt_s)
        Opt_=Opt_s
    End Property
    Private RURI

    Function AnalyzeChild(patrn,texts,ipos)
        Dim regEx, Match, Matches    
        Set regEx = New RegExp      
        regEx.IgnoreCase = true      
        regEx.Global = True          
        regEx.Pattern = patrn     
        regEx.Multiline = True 

        Dim RetStr
        Set Matches = regEx.Execute(texts)     
        If(Matches.Count > 0)Then RetStr= Matches(0).SubMatches(IPos)
       
        AnalyzeChild=RetStr
        Set regEx =Nothing
    End Function
    http://bizhi.VeVb.com
    Function getHTTPPage(url)
        dim objxml
        set objXML=server.createobject("MSXML2.xmlhttp")'定義
        objXML.open "GET",url,false'打開
        objXML.send()'發送
        If objXML.readystate<>4 then
            exit function
        End If
            getHTTPPage=BytesToBstr(objXML.responseBody)
        set objXML=nothing'關閉
        if err.number<>0 then err.Clear
    End Function

    Function BytesToBstr(body)
        dim objstream
        set objstream = Server.CreateObject("adodb.stream")
            objstream.Type = 1
            objstream.Mode =3
            objstream.Open
            objstream.Write body
            objstream.Position = 0
            objstream.Type = 2
            objstream.Charset = "utf-8" 
            '轉換原來默認的UTF-8編碼轉換成GB2312編碼,否則直接用XMLHTTP調用有中文字符的網頁得到的將是亂碼
            BytesToBstr = objstream.ReadText
        objstream.Close
        set objstream = nothing
    End Function
    Public Function GetText(str)
        If(isempty(str)) Then Exit Function
        Dim newUrl,Rs
        newUrl=Replace(Replace(RURI,"{0}",Server.URLEncode(Opt)),"{1}",Server.URLEncode(str))
        Rs=getHTTPPage(newUrl)
        GetText = AnalyzeChild("(<div id=result_box dir=""ltr"">)([?:/s/S]*?)(</div>)",Rs,1)
    End Function
    Sub class_Terminate

    End Sub
End Class

然后使用的時候:

Dim Obj
Set Obj = new GoogleTranslator
Obj.Opt="zh-CN|en"
response.write(Obj.GetText("我們"))

然后就可以了,需要注意的是因為Google的任何產品都是UTF-8格式的,這個asp文件保存為UTF-8格式,并在開頭加上:

<%@ LANGUAGE=VBScript CodePage=65001%>
<%Option Explicit
'... 開抄我上面的代碼

就可以了。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 碌曲县| 苏尼特右旗| 应城市| 南江县| 田阳县| 中阳县| 湖南省| 阳西县| 景洪市| 荥阳市| 临澧县| 长沙县| 洞口县| 应用必备| 特克斯县| 崇礼县| 四会市| 兴山县| 公主岭市| 东乌珠穆沁旗| 徐州市| 灵川县| 巫山县| 宁强县| 石家庄市| 新乡市| 平阴县| 广宁县| 青岛市| 元氏县| 明星| 古浪县| 玛多县| 镇雄县| 章丘市| 宣汉县| 榆林市| 措美县| 裕民县| 萨嘎县| 秀山|