ASP发起SOAP请求
Dim xml, http Set xml = Server.CreateObject("MSXML2.DOMDocument") xml.preserveWhiteSpace = true xml.async = false set http=server.createobject("MSXML2.XMLHTTP") http.open "POST","请求地址",false http.setRequestHeader "SOAPAction", 响应端提供的SOAPAction http.setRequestHeader "Content-Type", "text/xml;charset=utf-8" http.setRequestHeader "HOST","响应端域名或IP" http.setRequestHeader "Content-Length",发送xml内容长度 '非必须 http.send(要发送的xml) If http.readystate=4 then xml.Load(http.ResponseBody) 'xml.save(Server.MapPath("abc.xml")) '对接收到的xml进行解析 End If Set http=Nothing
asp生成CODE39码制的条形码
Function barCode(ByVal codeStr) 'code 39码制条形码 codeStr=UCase(codeStr) If emp(codeStr) Then Exit Function Else codeStr=UCase(cStr(codeStr)) Dim s : s=codeStr If Not regTest(s, "^[abcdefghijklmnopqrstuvwxyz1234567890 +\-%$./]{1,40}$") Then Exit Function Else s="*"&codeStr&"*" s=Replace(s,"0","_|_|__||_||_|") s=Replace(s,"1","_||_|__|_|_||") s=Replace(s,"2","_|_||__|_|_||") s=Replace(s,"3","_||_||__|_|_|") s=Replace(s,"4","_|_|__||_|_||") s=Replace(s,"5","_||_|__||_|_|") s=Replace(s,"7","_|_|__|_||_||") s=Replace(s,"6","_|_||__||_|_|") s=Replace(s,"8","_||_|__|_||_|") s=Replace(s,"9","_|_||__|_||_|") s=Replace(s,"A","_||_|_|__|_||") s=Replace(s,"B","_|_||_|__|_||") s=Replace(s,"C","_||_||_|__|_|") s=Replace(s,"D","_|_|_||__|_||") s=Replace(s,"E","_||_|_||__|_|") s=Replace(s,"F","_|_||_||__|_|") s=Replace(s,"G","_|_|_|__||_||") s=Replace(s,"H","_||_|_|__||_|") s=Replace(s,"I","_|_||_|__||_|") s=Replace(s,"J","_|_|_||__||_|") s=Replace(s,"K","_||_|_|_|__||") s=Replace(s,"L","_|_||_|_|__||") s=Replace(s,"M","_||_||_|_|__|") s=Replace(s,"N","_|_|_||_|__||") s=Replace(s,"O","_||_|_||_|__|") s=Replace(s,"P","_|_||_||_|__|") s=Replace(s,"Q","_|_|_|_||__||") s=Replace(s,"R","_||_|_|_||__|") s=Replace(s,"S","_|_||_|_||__|") s=Replace(s,"T","_|_|_||_||__|") s=Replace(s,"U","_||__|_|_|_||") s=Replace(s,"V","_|__||_|_|_||") s=Replace(s,"W","_||__||_|_|_|") s=Replace(s,"X","_|__|_||_|_||") s=Replace(s,"Y","_||__|_||_|_|") s=Replace(s,"Z","_|__||_||_|_|") s=Replace(s,"-","_|__|_|_||_||") s=Replace(s,"*","_|__|_||_||_|") s=Replace(s,"/","_|__|__|_|__|") s=Replace(s,"%","_|_|__|__|__|") s=Replace(s,"+","_|__|_|__|__|") s=Replace(s,".","_||__|_|_||_|") s=Replace(s," ","_|__||_|_||_|") s=Replace(s,"$","_|__|__|__|_|") s=Replace(s, "_", "<i></i>") s=Replace(s, "|", "<b></b>") barCode="<style>div.barcode{clear:both;margin:0;padding:0;width:auto !important;background:#fff;}div.barcode i,div.barcode b{display:block;float:left;height:50px;font-size:0;overflow:hidden;}div.barcode i{width:2px;background:#fff;}div.barcode b{width:0;border-left:2px solid #000;}div.barcode div{clear:both;font-family:verdana;font-size:14px;line-height:20px;letter-spacing:25px;color:#333;}</style><div class=""barcode"">"&s&"<div>"&codeStr&"</div></div>" End Function Function regTest(ByVal str, p) Dim re Set re = New RegExp re.Pattern = p re.IgnoreCase = true re.Global = true regTest = re.Test(str) End Function
asp xmlhttp发送form表单、xml、json或文本等示例
'发送端 Dim http set http=server.createobject("MSXML2.XMLHTTP") '下面一行只有在发送表单时才需要添加 'http.setRequestHeader "CONTENT-TYPE", "application/x-www-form-urlencoded" http.open "POST","http://127.0.0.1/respond.asp",false http.send(表单、文本、json、xml) If http.readystate=4 then '发送成功 '如果接收数据为乱码,需要用adodb.stream进行转码 '接收文本和json数据:text=http.ResponseText '接收xml:xml.Load(http.ResponseBody),只有响应端的内容类型是text/xml时,可以使用xml.Load(http.ResponseXML),也可以xml.LoadXML(http.ResponseText) end if Set http=Nothing '响应端 '接收表单直接用Request.Form '接收文本和json都是用adodb.stream对Request.BinaryRead(Request.TotalBytes)获取的二进制进行文本转换 '接收xml:xml.Load(Request.BinaryRead(Request.TotalBytes)) Dim xml Set xml = Server.CreateObject("MSXML2.DOMDocument") xml.preserveWhiteSpace = true xml.async = false 'xml.Load(二进制) xml.LoadXml(文本型xml) If xml.parseError.errorCode <> 0 Then Response.Write "Description: " & xml.parseError.reason & " Line: " & xml.parseError.Line Else If xml.getElementsByTagName("name").length=0 Then Response.Write "节点不存在" Else Response.Write xml.getElementsByTagName("name").Item(0).text End If End If
asp用服务器端js解析并获取简单json示例
<script type="text/javascript" language="javascript" runat="server"> function jsParseJSON(s){ try{return (new Function('return' + s))(); }catch(e){return false}; } function jsGetField(s, fld){ var obj=jsParseJSON(s); return obj===false ? '' : (typeof obj[fld]==='undefined' ? '' : obj[fld]); } </script> <% Response.Write jsGetField("{""err"":0,""msg"":""ok""}", "msg") %>输出结果:ok
ASP Scripting.Dictionary对象
Dim dict
Set dict = CreateObject("Scripting.Dictionary")
'添加项目
dict.Add "a", 100
dict.Add "b", 200
'统计项目数
Response.Write dict.Count
'删除项目
dict.Remove("a")
'判断项目是否存在
dict.exists("a")
'取关键字对应的值,注意在使用前需要判断是否存在key,否则dict中会多出一条记录
Response.Write dict.Item("a")
'修改关键字对应的值,如不存在则创建新的项目
dict.Item("a") = 101
'遍历
Dim arr, i
arr=dict.Items
For i=0 To dict.Count-1
Response.Write arr(i) '假设项目值为文本
Next
属性说明
CompareMode (仅用于VBScript)设定或返回键的字符串比较模式
Count 只读。返回Dictionary里的键/条目对的数量 ---从1开始,而不像数组从0开始计数
Item(key) 设定或返回指定的键的值
Key(key) 设定键名值
方法说明
Add(key,item) 增加键/条目对到Dictionary
Exists(key) 如果指定的键存在,返回True,否则返回False
Items() 返回一个包含Dictionary对象中所有条目的数组
Keys() 返回一个包含Dictionary对象中所有键的数组
Remove(key) 删除一个指定的键/条目对
RemoveAll() 删除全部键/条目对
Set dict = CreateObject("Scripting.Dictionary")
'添加项目
dict.Add "a", 100
dict.Add "b", 200
'统计项目数
Response.Write dict.Count
'删除项目
dict.Remove("a")
'判断项目是否存在
dict.exists("a")
'取关键字对应的值,注意在使用前需要判断是否存在key,否则dict中会多出一条记录
Response.Write dict.Item("a")
'修改关键字对应的值,如不存在则创建新的项目
dict.Item("a") = 101
'遍历
Dim arr, i
arr=dict.Items
For i=0 To dict.Count-1
Response.Write arr(i) '假设项目值为文本
Next
属性说明
CompareMode (仅用于VBScript)设定或返回键的字符串比较模式
Count 只读。返回Dictionary里的键/条目对的数量 ---从1开始,而不像数组从0开始计数
Item(key) 设定或返回指定的键的值
Key(key) 设定键名值
方法说明
Add(key,item) 增加键/条目对到Dictionary
Exists(key) 如果指定的键存在,返回True,否则返回False
Items() 返回一个包含Dictionary对象中所有条目的数组
Keys() 返回一个包含Dictionary对象中所有键的数组
Remove(key) 删除一个指定的键/条目对
RemoveAll() 删除全部键/条目对
在ASP中使用正则表达式
Dim arr
arr = regExec("192.168.a.1", "(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]{2}|[1-9])\.")
Response.Write Ubound(arr)&"<br><br>"
Set re = New RegExp
re.Pattern = p
re.IgnoreCase = true
re.Global = true
regTest = re.Test(str)
End Function
Function regReplace(ByVal str, p, str2)
If emp(str) Then
regReplace = ""
Else
Dim re
Set re = New RegExp
re.IgnoreCase = true
re.Global = true
re.Pattern = p
regReplace = re.Replace(str, str2)
End If
End Function
Function regMatch(Str, Pattern)
Dim regEx, obj
Set regEx = New RegExp
regEx.Pattern = Pattern
regEx.IgnoreCase = True
regEx.Global=True
Set obj = regEx.Execute(Str)
If obj.Count=0 Then
regMatch= array()
Else
Dim arr(), result, i
Redim arr(obj.count-1)
i=0
For Each result In obj
arr(i)=result
i=i+1
Next
regMatch=arr
End If
End Function
arr = regExec("192.168.a.1", "(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]{2}|[1-9])\.")
Response.Write Ubound(arr)&"<br><br>"
Response.Write regReplace("[IMG]http://www.baidu.com/img/baidu_sylogo1.gif[/IMG]", "\[IMG\](.+)\[\/IMG\]", "<img src=""$1"" />")
Function regTest(ByVal str, p) Dim reSet re = New RegExp
re.Pattern = p
re.IgnoreCase = true
re.Global = true
regTest = re.Test(str)
End Function
Function regReplace(ByVal str, p, str2)
If emp(str) Then
regReplace = ""
Else
Dim re
Set re = New RegExp
re.IgnoreCase = true
re.Global = true
re.Pattern = p
regReplace = re.Replace(str, str2)
End If
End Function
Function regMatch(Str, Pattern)
Dim regEx, obj
Set regEx = New RegExp
regEx.Pattern = Pattern
regEx.IgnoreCase = True
regEx.Global=True
Set obj = regEx.Execute(Str)
If obj.Count=0 Then
regMatch= array()
Else
Dim arr(), result, i
Redim arr(obj.count-1)
i=0
For Each result In obj
arr(i)=result
i=i+1
Next
regMatch=arr
End If
End Function