<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%> <% Response.CodePage=65001%> <% Response.Charset="UTF-8" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Asp Http Post Request</title> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body> <script language="JScript" runat="Server"> function toObject(json) { eval("var o=" + json); return o; } </script> <!-- #include file="json.asp" --> <% Function GetHttpPage(url, charset) Dim http Set http = Server.createobject("Microsoft.XMLHTTP") http.Open "GET", url, false http.Send() If http.readystate<>4 Then Exit Function End If GetHttpPage = BytesToStr(http.ResponseBody, charset) Set http = Nothing End Function Function PostHttpRequest(url,params,charset) Dim xmlHttp Set xmlHttp = Server.CreateObject("Microsoft.XMLHTTP") xmlHttp.open "POST ", url, False xmlHttp.setRequestHeader "Content-Type", "application/json" xmlHttp.setRequestHeader "User-Agent", "User-Agent:Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.8.1.14) Gecko/200804" xmlHttp.setRequestHeader "Connection", "keep-alive" xmlHttp.send params If xmlHttp.readystate<>4 Then Exit Function End If PostHttpRequest = BytesToStr(xmlHttp.ResponseBody, charset) Set xmlHttp = Nothing End Function Function BytesToStr(body, charset) 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 = charset BytesToStr = objStream.ReadText objStream.Close Set objStream = Nothing End Function %> <% Dim reqUrl reqUrl = "http://www.baidu.com" Dim reqCharset reqCharset = "UTF-8" If code <> "" Then Dim jString jString = PostHttpRequest(reqUrl,"{}",reqCharset) Dim jsonval Set jsonval = toObject(jString) %> <% End If %> <font size="2" color="red"> <br/> <br/> <br/> <br/> -----------------------运行本程序前确保以下组件存在------------------- <br/> Adodb.Stream (ADO 数据流对象, 常见被用在无组件上传程序中) √ <br/> Microsoft.XMLHTTP (Http 组件, 常在采集系统中用到) √ <br/> 服务器脚本引擎 VBScript/5.8.23141 , JScript/5.8.23259 √ <br/> ----------------------------------------------------------------------------------------- <br/> </font> </body> </html>
提供一个测试asp发送http request(get/post),并且可以将数据发送并接收的例子。传输的数据格式使用json。附件中有完整代码。