代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
反向代理,可以缓存
作者:
foxidea
/ 发布于
2013/1/8
/
971
<%@ WebHandler Language="C#" Class="proxy" %> using System; using System.Web; using System.Net; using System.Text; using System.IO; using System.Collections.Generic; using System.Configuration; /// <summary> /// 把http headers 和 http 响应的内容 分别存储在 /proxy/header/ 和 /proxy/body/ 中 /// 分层次创建目录 /// </summary> public class proxy : IHttpHandler { HttpResponse Response; HttpRequest Request; HttpApplicationState Application; HttpServerUtility Server; static string proxyCacheFolder = ConfigurationManager.AppSettings["proxyCacheFolder"]; static string proxyDomain = ConfigurationManager.AppSettings["proxyDomain"]; static string proxyReferer = ConfigurationManager.AppSettings["proxyReferer"]; bool proxyCacheDirectAccess = ConfigurationManager.AppSettings["proxyCacheDirectAccess"] == "true"; int proxyCacheSeconds = int.Parse(ConfigurationManager.AppSettings["proxyCacheSeconds"]); public void ProcessRequest(HttpContext context) { Response = context.Response; Request = context.Request; Application = context.Application; Server = context.Server; string path = context.Request.RawUrl; bool delCache = path.IndexOf("?del") > 0; if (delCache) { path = path.Replace("?del", string.Empty); DeleteCacheFile(path); return; } bool allowCache = Request.QueryString["cache"] == "true"; string seconds = Request.QueryString["seconds"] ?? string.Empty; if (!int.TryParse(seconds, out proxyCacheSeconds)) { proxyCacheSeconds = 3600; } if (allowCache) { EchoData(path); } else { WebClient wc = new WebClient(); wc.Headers.Set("Referer", proxyReferer); byte[] buffer = wc.DownloadData(proxyDomain + path); Response.ContentType = wc.ResponseHeaders["Content-Type"]; foreach (string key in wc.ResponseHeaders.AllKeys) { Response.Headers.Set(key, wc.ResponseHeaders[key]); } wc.Dispose(); Response.OutputStream.Write(buffer, 0, buffer.Length); } } /// <summary> /// 清理失效的缓存 /// </summary> /// <param name="d"></param> void ClearTimeoutCache(DirectoryInfo d) { if (d.Exists) { FileInfo[] files = d.GetFiles(); foreach (FileInfo file in files) { TimeSpan timeSpan = DateTime.Now - file.LastAccessTime; if (timeSpan.TotalSeconds > proxyCacheSeconds) { file.Delete(); } } } } string GetCacheFolderPath(string hash) { string s = string.Empty; for (int i = 0; i <= 2; i++) { s += hash[i] + "/"; } return s; } /// <summary> /// 读取缓存的header 并输出 /// </summary> /// <param name="cacheHeaderPath"></param> void EchoCacheHeader(string cacheHeaderPath) { string[] headers = File.ReadAllLines(cacheHeaderPath); for (int i = 0; i < headers.Length; i++) { string[] headerKeyValue = headers[i].Split(':'); if (headerKeyValue.Length == 2) { if (headerKeyValue[0] == "Content-Type") { Response.ContentType = headerKeyValue[1]; } Response.Headers.Set(headerKeyValue[0], headerKeyValue[1]); } } } void DeleteCacheFile(string path) { string absFolder = Server.MapPath(proxyCacheFolder); string hash = GetHashString(path); string folder = GetCacheFolderPath(hash); string cacheBodyPath = absFolder + "/body/" + folder + hash; string cacheHeaderPath = absFolder + "/header/" + folder + hash; FileInfo cacheBody = new FileInfo(cacheBodyPath); FileInfo cacheHeader = new FileInfo(cacheHeaderPath); if (cacheBody.Exists) { cacheBody.Delete(); } if (cacheHeader.Exists) { cacheHeader.Delete(); } Response.Write("delete cache file Success!\r\n" + path); } /// <summary> /// 输出缓存 /// </summary> /// <param name="cacheHeaderPath">缓存header 的文件路径</param> /// <param name="cacheBodyPath">缓存 body 的文件路径</param> /// <param name="ifTimeout">是否进行判断文件过期</param> /// <returns>是否输出成功</returns> bool EchoCacheFile(string cacheHeaderPath, string cacheBodyPath, bool ifTimeout) { FileInfo cacheBody = new FileInfo(cacheBodyPath); FileInfo cacheHeader = new FileInfo(cacheHeaderPath); ClearTimeoutCache(cacheBody.Directory); ClearTimeoutCache(cacheHeader.Directory); if (cacheBody.Exists && cacheHeader.Exists) { if (ifTimeout) { TimeSpan timeSpan = DateTime.Now - cacheBody.LastWriteTime; if (timeSpan.TotalSeconds < proxyCacheSeconds) { EchoCacheHeader(cacheHeaderPath); Response.TransmitFile(cacheBodyPath); return true; } } else { EchoCacheHeader(cacheHeaderPath); Response.TransmitFile(cacheBodyPath); return true; } } return false; } void EchoData(string path) { string absFolder = Server.MapPath(proxyCacheFolder); string hash = GetHashString(path); string folder = GetCacheFolderPath(hash); string cacheBodyPath = absFolder + "/body/" + folder + hash; string cacheHeaderPath = absFolder + "/header/" + folder + hash; bool success; if (proxyCacheDirectAccess) { success = EchoCacheFile(cacheHeaderPath, cacheBodyPath, false); if (!success) { Response.Write("直接从缓存读取失败!"); } return; } success = EchoCacheFile(cacheHeaderPath, cacheBodyPath, true); if (success) { return; } //更新Cache File string ApplicationKey = "CacheList"; List<string> List = null; if (Application[ApplicationKey] == null) { Application.Lock(); Application[ApplicationKey] = List = new List<string>(1000); Application.UnLock(); } else { List = (List<string>)Application[ApplicationKey]; } //判断是否已有另一个进程正在更新Cache File if (List.Contains(hash)) { success = EchoCacheFile(cacheHeaderPath, cacheBodyPath, false); if (success) { return; } else { WebClient wc = new WebClient(); wc.Headers.Set("Referer", proxyReferer); //主体内容 byte[] data = wc.DownloadData(proxyDomain + path); //处理header Response.ContentType = wc.ResponseHeaders["Content-Type"]; foreach (string key in wc.ResponseHeaders.AllKeys) { Response.Headers.Set(key, wc.ResponseHeaders[key]); } wc.Dispose(); Response.BinaryWrite(data); } } else { WebClient wc = new WebClient(); wc.Headers.Set("Referer", proxyReferer); StringBuilder headersb = new StringBuilder(); List.Add(hash); //主体内容 byte[] data = wc.DownloadData(proxyDomain + path); //处理header Response.ContentType = wc.ResponseHeaders["Content-Type"]; foreach (string key in wc.ResponseHeaders.AllKeys) { headersb.Append(key); headersb.Append(":"); headersb.Append(wc.ResponseHeaders[key]); headersb.Append("\r\n"); Response.Headers.Set(key, wc.ResponseHeaders[key]); } wc.Dispose(); string headers = headersb.ToString().Trim(); if (!Directory.Exists(absFolder + "/header/" + folder)) { Directory.CreateDirectory(absFolder + "/header/" + folder); } StreamWriter sw = File.CreateText(absFolder + "/header/" + folder + hash); sw.Write(headers); sw.Close(); sw.Dispose(); //处理缓存内容 if (!Directory.Exists(absFolder + "/body/" + folder)) { Directory.CreateDirectory(absFolder + "/body/" + folder); } FileStream fs = File.Create(absFolder + "/body/" + folder + hash); fs.Write(data, 0, data.Length); fs.Close(); fs.Dispose(); List.Remove(hash); Response.BinaryWrite(data); } } string GetHashString(string path) { string md5 = GetMd5Str(path); return md5; } static string GetMd5Str(string ConvertString) { System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8); t2 = t2.Replace("-", ""); return t2; } public bool IsReusable { get { return false; } } }
试试其它关键字
反向代理
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
foxidea
贡献的其它代码
(
4
)
.
判断客户端是否为手机移动设备
.
判断是否为 HTTPS 并跳转
.
正则表达式验证手机号码
.
反向代理,可以缓存
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3