当前位置:编程学习 > VB >>

VBS的函数集-读写二进制文件,UTF8文件,HTTP GET和POST。

[vb] 
url1 = "http://xxx.xxx.xxx:8081/incity/brand/5020bb58718b7b4a3ef3eadc/subscription" 
url2 = "http://xxx.xxx.xxx:8081/incity/brands?level=2" 
Call Main 
Sub Main() 
    'Call GetHttp(url2) 
    Call WriteFileUTF8("c:\utf8.txt", "UTF8字符串") 
    'Call PostHttp(url1) 
End Sub 
 
''''''''''''''''''''''''以下是函数定义'''''''''''''''''''''''''''''''''''''' 
 
' Valid Charset values for ADODB.Stream 
Const CdoBIG5        = "big5" 
Const CdoEUC_JP      = "euc-jp" 
Const CdoEUC_KR      = "euc-kr" 
Const CdoGB2312      = "gb2312" 
Const CdoISO_2022_JP = "iso-2022-jp" 
Const CdoISO_2022_KR = "iso-2022-kr" 
Const CdoISO_8859_1  = "iso-8859-1" 
Const CdoISO_8859_2  = "iso-8859-2" 
Const CdoISO_8859_3  = "iso-8859-3" 
Const CdoISO_8859_4  = "iso-8859-4" 
Const CdoISO_8859_5  = "iso-8859-5" 
Const CdoISO_8859_6  = "iso-8859-6" 
Const CdoISO_8859_7  = "iso-8859-7" 
Const CdoISO_8859_8  = "iso-8859-8" 
Const CdoISO_8859_9  = "iso-8859-9" 
Const cdoKOI8_R      = "koi8-r" 
Const cdoShift_JIS   = "shift-jis" 
Const CdoUS_ASCII    = "us-ascii" 
Const CdoUTF_7       = "utf-7" 
Const CdoUTF_8       = "utf-8" 
 
' ADODB.Stream file I/O constants 
Const adTypeBinary          = 1 
Const adTypeText            = 2 
Const adSaveCreateNotExist  = 1 
Const adSaveCreateOverWrite = 2 
 
Function GetHttp(url) 
    Set xmlhttp = CreateObject("Msxml2.ServerXMLHTTP")  
    postdata = "" 
    xmlhttp.Open "GET", url, False 
    xmlhttp.setRequestHeader "Authorization", "Basic " & Base64encode("test:pass") 
    'xmlhttp.setRequestHeader("Referer","来路的绝对地址") 
    'xmlhttp.setRequestHeader "Cookie",Cookies   'Cookie 
    xmlhttp.Send postdata 
    Wscript.echo xmlhttp.status & ":" & xmlhttp.statusText 
    respStr = BytesToBstr(xmlhttp.responseBody, "UTF-8") 
    Wscript.echo respStr 
    set xmlhttp = nothing 
End Function 
 
Function PostHttp(url) 
    Set xmlhttp = CreateObject("Msxml2.ServerXMLHTTP")  
    postdata = "" 
    xmlhttp.Open "POST", url1, False 
    xmlhttp.setRequestHeader "CONTENT-TYPE","application/x-www-form-urlencoded" 
    xmlhttp.setRequestHeader "Authorization", "Basic " & Base64encode("test:pass") 
    'xmlhttp.setRequestHeader("Referer","来路的绝对地址") 
    'xmlhttp.setRequestHeader "Cookie",Cookies   'Cookie 
    xmlhttp.Send postdata 
    Wscript.echo xmlhttp.status & ":" & xmlhttp.statusText 
    respStr = BytesToBstr(xmlhttp.responseBody, "GB2312") 
    Wscript.echo respStr 
    set xmlhttp = nothing 
End Function 
 
Function Str2Bytes(str,charset) 
    Dim ms,strRet 
    Set ms = CreateObject("ADODB.Stream")    '建立流对象 
        ms.Type = 2             ' Text 
        ms.Charset = charset    '设置流对象的编码方式为 charset 
        ms.Open                     
        ms.WriteText str            '把str写入流对象中         
        ms.Position = 0         '设置流对象的起始位置是0 以设置Charset属性 
        ms.Type = 1              'Binary 
        vout = ms.Read(ms.Size)    '取字符流 
        ms.close                '关闭流对象 
    Set ms = nothing 
    Str2Bytes = vout 
End Function 
 
Function BytesToBstr(strBody,CodeBase) 
    If lenb(strBody) = 0 Then  
        BytesToBstr = "" 
        Exit Function 
    End If 
    dim objStream 
    set objStream = CreateObject("Adodb.Stream") 
    objStream.Type = 1 
    objStream.Mode =3 
    objStream.Open 
    objStream.Write strBody 
    objStream.Position = 0 
    objStream.Type = 2 
    objStream.Charset = CodeBase 
    BytesToBstr = objStream.ReadText 
    objStream.Close 
    set objStream = nothing 
End Function 
 
Function URLEncoding(vstrIn) 
    strReturn = "" 
    For i = 1 To Len(vstrIn) 
        ThisChr = Mid(vStrIn,i,1) 
        If Abs(Asc(ThisChr)) < &HFF Then 
            strReturn = strReturn & ThisChr 
        Else 
            in
补充:Web开发 , 其他 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,