ASP Request

QueryString / Form / Cookies / ServerVariables / ClientCertificate 五个集合。

Intermediate
Key PointsRequest 有五个集合:URL 参数、表单、Cookie、服务器变量、客户端证书;用集合名显式取值最安全。

五个集合

CollectionSourceExample
QueryString?a=1&b=2Request.QueryString("a")
FormPOST 请求体Request.Form("name")
Cookies请求头 CookieRequest.Cookies("theme")
ServerVariables请求头与服务器信息Request.ServerVariables("REMOTE_ADDR")
ClientCertificateSSL 客户端证书很少用

QueryString

ASP
<%
    ' /page.asp?id=42&tag=asp&tag=web
    Response.Write Request.QueryString("id") & "<br>"
    Response.Write Request.QueryString("tag").Count & " 个 tag<br>"
    Response.Write Request.QueryString("tag")(1) & "<br>"

    ' 遍历所有参数
    Dim k
    For Each k In Request.QueryString
        Response.Write k & " = " & Request.QueryString(k) & "<br>"
    Next
%>
Output
42
2 个 tag
asp
id = 42
tag = asp, web

When a parameter repeats, Request.QueryString("tag") returns asp, web; index (0), (1) for individual values. Use .Count instead of assuming a single value.

ServerVariables

ASP
<%
    Response.Write "客户端 IP:" & Request.ServerVariables("REMOTE_ADDR") & "<br>"
    Response.Write "请求方法:" & Request.ServerVariables("REQUEST_METHOD") & "<br>"
    Response.Write "请求路径:" & Request.ServerVariables("PATH_INFO") & "<br>"
    Response.Write "查询串:" & Request.ServerVariables("QUERY_STRING") & "<br>"
    Response.Write "User-Agent:" & Request.ServerVariables("HTTP_USER_AGENT") & "<br>"
    Response.Write "来源页:" & Request.ServerVariables("HTTP_REFERER") & "<br>"
    Response.Write "服务器名:" & Request.ServerVariables("SERVER_NAME")
%>
Output
客户端 IP:203.0.113.42
请求方法:GET
请求路径:/page.asp
查询串:id=42
User-Agent:Mozilla/5.0 ...
来源页:https://example.com/list
服务器名:www.example.com

Headers appear with an HTTP_ prefix and - replaced by _: User-Agent becomes HTTP_USER_AGENT. A few such as CONTENT_TYPE and CONTENT_LENGTH have no prefix.

拿真实客户端 IP

ASP
<%
    Function ClientIP()
        Dim ip
        ip = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
        If ip = "" Then ip = Request.ServerVariables("REMOTE_ADDR")
        ' X-Forwarded-For 可能是 "客户端, 代理1, 代理2"
        If InStr(ip, ",") > 0 Then ip = Trim(Split(ip, ",")(0))
        ClientIP = ip
    End Function
%>

Behind a proxy or CDN, REMOTE_ADDR is the proxy's address; the real client is the first value in X-Forwarded-For. That header can be forged, so trust it only when your proxy overwrites it.

安全提醒

ASP
<%
    ' 危险:直接拼进 SQL
    sql = "SELECT * FROM users WHERE name = '" & Request.Form("name") & "'"

    ' 危险:直接输出到 HTML
    Response.Write Request.Form("comment")

    ' 安全:转义后输出
    Response.Write Server.HTMLEncode(Request.Form("comment"))

    ' 安全:参数化查询(见 ADO 章节)
%>
Every value from Request is user-controlled: concatenating into SQL is injection, printing is XSS, concatenating into a path is traversal. Validate or escape before use.

读取原始请求体

ASP
<%
    Dim body
    body = Request.BinaryRead(Request.TotalBytes)
%>

BinaryRead reads the raw body (useful for JSON) but cannot be mixed with Request.Form: once the raw body is read the Form collection is empty. Pick one.

Friends & Partners