- 登录验证:创建一个登录页面,验证用户名和密码。
- 源码下载:创建一个需要登录后才能访问的页面,该页面负责获取目标网页的源码并提供下载。
准备工作:Web 服务器环境
你需要一个支持 ASP 的 Web 服务器环境,最常见和推荐的是使用 IIS (Internet Information Services) 在 Windows 系统上搭建,确保你的 IIS 已经安装并配置好 ASP 支持。

(图片来源网络,侵删)
第一步:创建登录页面 (login.asp)
这个页面包含一个登录表单,用户输入用户名和密码后提交。
login.asp
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">用户登录</title>
<style>
body { font-family: Arial, sans-serif; background-color: #f4f4f4; margin: 0; padding: 20px; }
.container { max-width: 400px; margin: auto; background: white; padding: 20px; border-radius: 5px; box-shadow: 0 0 10px rgba(0,0,0,0.1); }
input[type="text"], input[type="password"] { width: 100%; padding: 10px; margin: 10px 0; border: 1px solid #ddd; box-sizing: border-box; }
input[type="submit"] { background-color: #4CAF50; color: white; padding: 10px 15px; border: none; cursor: pointer; width: 100%; }
.error { color: red; margin-bottom: 10px; }
</style>
</head>
<body>
<div class="container">
<h2>用户登录</h2>
<%
' 检查是否有登录失败的提示信息
If Request.QueryString("error") = "1" Then
%>
<div class="error">用户名或密码错误!</div>
<%
End If
%>
<form action="check_login.asp" method="post">
<div>
<label for="username">用户名:</label>
<input type="text" id="username" name="username" required>
</div>
<div>
<label for="password">密码:</label>
<input type="password" id="password" name="password" required>
</div>
<div>
<input type="submit" value="登录">
</div>
</form>
</div>
</body>
</html>
第二步:处理登录逻辑 (check_login.asp)
这个页面接收 login.asp 提交的表单数据,进行验证,如果验证通过,则创建一个 Session 并重定向到下载页面;如果失败,则返回登录页面并提示错误。
check_login.asp

(图片来源网络,侵删)
<%@ Language=VBScript %>
<%
' 禁止页面缓存,防止后退时看到登录前的内容
Response.Buffer = True
Response.ExpiresAbsolute = Now() - 1
Response.Expires = 0
Response.CacheControl = "no-cache"
' 获取表单提交的数据
Dim username, password
username = Request.Form("username")
password = Request.Form("password")
' --- 在这里设置你的用户名和密码 ---
' 为了安全,实际项目中应该从数据库或配置文件中读取
Dim validUsername, validPassword
validUsername = "admin"
validPassword = "password123"
' 验证用户名和密码
If username = validUsername And password = validPassword Then
' 验证成功,创建一个 Session 变量来标记用户已登录
Session("isLoggedIn") = True
Session("username") = username
' 重定向到下载页面
Response.Redirect "download_source.asp"
Else
' 验证失败,重定向回登录页面,并传递一个错误参数
Response.Redirect "login.asp?error=1"
End If
%>
重要安全提示:
- 上面的代码中,用户名和密码是硬编码在 ASP 文件中的,这非常不安全。
- 在实际生产环境中,你应该将用户凭据存储在数据库(如 SQL Server, Access)中,并进行哈希(如 MD5, SHA256)处理后比较。
- 这里为了演示的简洁性,我们才使用硬编码。
第三步:创建源码下载页面 (download_source.asp)
这是核心功能页面,它会首先检查用户是否已登录,如果已登录,则获取目标网页的源码,并将其作为文件提供给用户下载。
download_source.asp
<%@ Language=VBScript %>
<%
' 禁止页面缓存
Response.Buffer = True
Response.ExpiresAbsolute = Now() - 1
Response.Expires = 0
Response.CacheControl = "no-cache"
' 1. 检查用户是否已登录
If Not Session("isLoggedIn") Then
' 如果未登录,重定向到登录页面
Response.Redirect "login.asp"
Response.End ' 停止执行后续代码
End If
' 2. 定义要下载的目标网页URL
' 注意:如果目标URL是跨域的,且该网站没有配置CORS允许你的服务器访问,
' 或者你的服务器没有代理功能,则可能会失败,这里我们以一个公开的网站为例。
Dim targetUrl
targetUrl = "https://www.example.com" ' <--- 修改成你想要下载源码的网页地址
' 3. 使用 ServerXMLHTTP 对象获取网页源码
Dim xmlHttp, sourceCode
Set xmlHttp = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")
On Error Resume Next ' 启用错误处理,以防URL无效或网络问题
xmlHttp.Open "GET", targetUrl, False
xmlHttp.Send()
If Err.Number <> 0 Then
' 如果发生错误,显示错误信息
Response.Write "<h2>错误</h2>"
Response.Write "<p>无法获取网页源码,请检查URL是否正确或服务器网络是否正常。</p>"
Response.Write "<p>错误详情: " & Err.Description & "</p>"
Response.Write "<p><a href='javascript:history.back()'>返回</a></p>"
Set xmlHttp = Nothing
Response.End
End If
On Error GoTo 0 ' 关闭错误处理
' 获取响应状态码,200表示成功
If xmlHttp.Status = 200 Then
sourceCode = xmlHttp.responseText
Else
Response.Write "<h2>错误</h2>"
Response.Write "<p>服务器返回错误状态码: " & xmlHttp.Status & " " & xmlHttp.StatusText & "</p>"
Set xmlHttp = Nothing
Response.End
End If
Set xmlHttp = Nothing ' 释放对象
' 4. 设置响应头,使浏览器弹出下载对话框
' 使用一个有意义的文件名
Dim fileName
fileName = "source_page.html"
Response.ContentType = "text/html" ' 告诉浏览器下载的是HTML文件
Response.AddHeader "Content-Disposition", "attachment; filename=" & fileName
Response.AddHeader "Content-Length", Len(sourceCode)
' 5. 输出源码,触发下载
Response.Write sourceCode
%>
第四步:创建一个简单的欢迎页面 (welcome.asp)
为了让流程更完整,我们创建一个登录成功后可以访问的欢迎页面,并从那里链接到下载页面。

(图片来源网络,侵删)
welcome.asp
<%@ Language=VBScript %>
<%
' 检查登录状态
If Not Session("isLoggedIn") Then
Response.Redirect "login.asp"
Response.End
End If
%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">欢迎页面</title>
<style>
body { font-family: Arial, sans-serif; background-color: #f4f4f4; margin: 0; padding: 20px; text-align: center; }
.container { max-width: 600px; margin: auto; background: white; padding: 20px; border-radius: 5px; box-shadow: 0 0 10px rgba(0,0,0,0.1); }
a { display: inline-block; margin-top: 20px; padding: 10px 20px; background-color: #007bff; color: white; text-decoration: none; border-radius: 5px; }
</style>
</head>
<body>
<div class="container">
<h1>欢迎, <%= Session("username") %>!</h1>
<p>您已成功登录。</p>
<p>现在您可以下载指定网页的源代码了。</p>
<a href="download_source.asp">点击下载网页源码</a>
<br><br>
<a href="logout.asp">退出登录</a>
</div>
</body>
</html>
第五步:创建退出登录页面 (logout.asp)
这个页面用于销毁 Session,让用户安全退出。
logout.asp
<%@ Language=VBScript %>
<%
' 销毁 Session
Session.Abandon()
' 重定向到登录页面
Response.Redirect "login.asp"
%>
总结与部署
- 文件结构:将以上五个文件 (
login.asp,check_login.asp,download_source.asp,welcome.asp,logout.asp) 放置在你的 IIS 网站根目录下(C:\inetpub\wwwroot\)。 - 配置目标URL:打开
download_source.asp文件,将targetUrl变量的值修改为你想要获取源码的真实网页地址。 - 访问:在浏览器中访问
http://localhost/(或你的网站域名),系统会自动重定向到login.asp。 - 测试流程:
- 使用错误的用户名/密码登录,应提示错误并停留在登录页。
- 使用正确的用户名/密码(
admin/password123)登录,应成功跳转到welcome.asp。 - 在
welcome.asp页面点击“点击下载网页源码”,浏览器会弹出一个下载对话框,下载的文件名是source_page.html就是你指定的targetUrl的网页源码。
重要注意事项
- 权限问题:
ServerXMLHTTP组件在 IIS 中默认是启用的,但如果遇到权限问题(拒绝访问”错误),可能需要检查 IIS 应用程序池的权限,或者使用更高级的组件如WinHttp.WinHttpRequest.5.1。 - 跨域限制:如果你尝试获取的网站设置了严格的 CORS (跨域资源共享) 策略,直接从你的 ASP 服务器请求可能会失败。
ServerXMLHTTP不受浏览器同源策略的限制,但受目标服务器的服务器端策略限制。 - 性能与超时:对于响应非常慢或很大的网站,
xmlHttp.Send()可能会超时,你可以设置超时时间:xmlHttp.setTimeouts 5000, 5000, 10000, 10000(分别对应 resolve, connect, send, receive 的毫秒数)。 - 编码问题:如果目标网页的编码不是 UTF-8,你获取到的源码可能会出现乱码,可以检查
xmlHttp.getResponseHeader("Content-Type")来获取原始编码,并使用ADODB.Stream等组件进行正确的编码转换,这是一个更高级的话题。
