在前面的文章《涂鸦代理:强行引用反盗链图片》中,给出了网页代理的代码。
今天做了一些改进,对于返回的图片,给出它原来的实际文件名,而不是统一的 "OpenItWithYourPhotoViewer.jpg"。
如下:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<% Option Explicit %>
<%Session.CodePage=65001%>
<%
Response.CharSet = "utf-8"
%>
<%
Dim url, oHTTP, charset, contentType
url = Request.QueryString("url")
charset = Request.QueryString("charset")
contentType = Request.QueryString("contentType")
If Len(contentType) <= 0 Then
contentType = "text/html"
End If
Response.ContentType = contentType
If Len(charset) <= 0 Then
charset = "utf-8"
End If
If Len(url) > 0 Then
Set oHTTP = Server.CreateObject("Microsoft.XMLHTTP")
oHTTP.Open "GET", url, False
On Error Resume Next
oHTTP.Send
If Err.Number <> 0 Then
Session("ssnErrorNumber") = Err.Number
Session("ssnErrorDescription") = Err.Description
Session("ssnErrorSource") = Err.Source
End If
On Error Goto 0
if contentType = "text/html" Then
If charset = "utf-8" Then
Response.Write oHTTP.ResponseText
Else
Response.Write Cbns2TextStream(oHTTP.ResponseBody, charset)
End If
Else
Dim fileName
fileName = Right(url, Len(url) - InStrRev(url, "/"))
'Response.AddHeader "Content-Disposition", "attachment; filename=" & "OpenItWithYourPhotoViewer.jpg"
Response.AddHeader "Content-Disposition", "attachment; filename=" & fileName
Response.BinaryWrite oHTTP.ResponseBody
End If
Set oHTTP = Nothing
If Session("ssnErrorNumber") <> 0 Then
Server.Transfer "error/Default.asp"
End If
Else
Response.Write ""
End If
%>
<%
'
' 将指定的二进制串转换成特定编码的文本
'
Public Function Cbns2TextStream(ByRef bns, ByRef sCharset)
Dim stm
Set stm = Server.CreateObject("ADODB.Stream")
stm.Type = 2
stm.Open
stm.WriteText bns
stm.Position = 0
If Len(sCharset) > 0 Then stm.Charset = sCharset
Cbns2TextStream = stm.ReadText
stm.Close
Set stm = Nothing
End Function
%>