<%@ WebHandler Language="C#" Class="webproxy" %>
using System;
using System.Web;
using System.Net;
using System.Xml.Serialization;
public class webproxy : IHttpHandler
{
HttpResponse Response;
HttpRequest Request;
public Uri url;
public void ProcessRequest(HttpContext context)
{
this.Response = context.Response;
this.Request = context.Request;
string qs = context.Request.Url.Query;
if (string.IsNullOrEmpty(qs)
[解决办法]
qs.Length < 10)
{
context.Response.StatusCode = 404;
return;
}
ExpireProc();
qs = qs.Substring(1);
if (!qs.StartsWith("http://", StringComparison.OrdinalIgnoreCase))
{
Uri rdURI = GetRequestUrl(qs);
if (System.Text.RegularExpressions.Regex.IsMatch(rdURI.AbsolutePath,
"\\.(js
[解决办法]
jpg
[解决办法]
png
[解决办法]
gif
[解决办法]
css
[解决办法]
bmp)$", System.Text.RegularExpressions.RegexOptions.IgnoreCase))
{//一般非导航页面
this.url = rdURI;
}
else
{//一般性用于导航
Response.Redirect("?" + GetRequestUrl(qs).ToString());
return;
}
}
else
this.url = new Uri(qs);
string reffer = null;
if (Request.UrlReferrer != null)
{
reffer = Request.UrlReferrer.Query;
if (!string.IsNullOrEmpty(reffer))
reffer = reffer.Substring(1);
}
HttpWebRequest web = (HttpWebRequest)WebRequest.Create(this.url);
web.Accept = string.Join(", ", Request.AcceptTypes);
web.Headers["Accept-Encoding"] = "gzip";
string tValue;
tValue = Request.Headers["Accept-Language"];
if (!string.IsNullOrEmpty(tValue))
web.Headers["Accept-Language"] = tValue;
web.AllowAutoRedirect = true;
web.CookieContainer = Cookies;
web.UserAgent = Request.UserAgent;
web.Referer = reffer;
web.AutomaticDecompression = DecompressionMethods.GZip;
web.Timeout = 2000;
WebResponse rsp = null;
try
{
rsp = web.GetResponse();
}
catch (WebException ex)
{
Response.StatusCode = (int)ex.Status;
rsp = ex.Response;
}
catch (Exception ex)
{
Response.ContentType = "text/txtfile";
Response.Write(ex.ToString());
return;
}
if (rsp == null)
{
return;
}
System.Text.Encoding ec = System.Text.ASCIIEncoding.Default;
if (!string.IsNullOrEmpty(rsp.ContentType))
{
Response.ContentType = rsp.ContentType;
System.Text.RegularExpressions.Match mc = System.Text.RegularExpressions.Regex.Match(rsp.ContentType, "charset=([^;]+)");
if (mc.Success)
ec = System.Text.Encoding.GetEncoding(mc.Groups[1].Value);
}
if (rsp.Headers["Set-Cookie"] != null)
{
Cookies = web.CookieContainer;
}
using (System.IO.Stream stm = rsp.GetResponseStream())
{
string ct = rsp.ContentType;
if (string.IsNullOrEmpty(ct))
ct = "text/html";
if (!ct.Contains("text/css") && !ct.Contains("text/html"))
{
Response.Cache.SetLastModified(DateTime.Now);
Response.Cache.SetExpires(DateTime.Now.AddMinutes(20));
TranslateDirect(stm);
return;
}
Response.Cache.SetLastModified(DateTime.Now);
Response.Cache.SetExpires(DateTime.Now.AddMinutes(5));
Response.ContentEncoding = ec;
System.IO.StreamReader reader = new System.IO.StreamReader(stm, ec);
string html = reader.ReadToEnd();
html = System.Text.RegularExpressions.Regex.Replace(html, @"(<(a
[解决办法]
link)[^>]+href[^\w\/\\]{1,10})", "$1webproxy.ashx?");
html = System.Text.RegularExpressions.Regex.Replace(html, @"(<(img
[解决办法]
script
[解决办法]
iframe)[^>]+src[^\w\/\\]{1,10})", "$1webproxy.ashx?");
html = System.Text.RegularExpressions.Regex.Replace(html, @"(background(
[解决办法]
-image)\: {0,}url\([^\w\/\\]{0,1})", "$1webproxy.ashx?");
Response.Write(html);
}
}
void TranslateDirect(System.IO.Stream stm)
{
Response.BufferOutput = false;
byte[] bin = new byte[4096];
int r;
do
{
r = stm.Read(bin, 0, 4096);
if (r > 0)
{
if (!Response.IsClientConnected)
break;
Response.OutputStream.Write(bin, 0, r);
Response.Flush();
}
} while (r != 0);
}
#region Cookies
CookieContainer _cc = null;
CookieContainer Cookies
{
get
{
return null;
if (_cc != null)
return _cc;
string cID = Convert.ToBase64String(System.Text.ASCIIEncoding.Default.GetBytes(this.url.Host));
cID = cID.Replace('=', '_');
HttpCookie cook = Request.Cookies[cID];
if (cook == null)
return new CookieContainer();
byte[] bin = Convert.FromBase64String(cook.Value);
if (bin == null)
return null;
using (System.IO.MemoryStream stm = new System.IO.MemoryStream(bin))
{
using (System.IO.Compression.GZipStream zip = new System.IO.Compression.GZipStream(stm, System.IO.Compression.CompressionMode.Decompress))
{
XmlSerializer XS = new XmlSerializer(typeof(CookieContainer));
return (CookieContainer)XS.Deserialize(zip);
}
}
}
set
{
return;
_cc = value;
string cID = Convert.ToBase64String(System.Text.ASCIIEncoding.Default.GetBytes(this.url.Host));
cID = cID.Replace('=', '_');
HttpCookie cookie = new HttpCookie(cID);
Response.Cookies.Add(cookie);
if (value == null)
{
cookie.Expires = DateTime.Now.AddYears(-1);
return;
}
using (System.IO.MemoryStream stm = new System.IO.MemoryStream())
{
using (System.IO.Compression.GZipStream zip = new System.IO.Compression.GZipStream(stm, System.IO.Compression.CompressionMode.Compress))
{
XmlSerializer XS = new XmlSerializer(typeof(CookieContainer));
XS.Serialize(zip, value);
zip.Flush();
cookie.Value = Convert.ToBase64String(stm.ToArray());
cookie.Expires = DateTime.Now.AddDays(1);
}
}
}
}
#endregion
Uri GetRequestUrl(string offset)
{
Uri src = Request.UrlReferrer;
if (src == null)
return null;
string qs = src.Query;
if (string.IsNullOrEmpty(qs)
[解决办法]
qs.Length < 10)
return null;
src = new Uri(qs.Substring(1));
return new Uri(src, offset);
}
/// <summary>
/// 过期信息检查并处理
/// </summary>
void ExpireProc()
{
string t = Request.Headers["If-Modified-Since"];
if (string.IsNullOrEmpty(t))
return;
DateTime last;
if (!DateTime.TryParse(t, out last))
return;
if ((DateTime.Now - last).TotalMinutes > 20)
return;
Response.StatusCode = 304;
Response.End();
}
public bool IsReusable
{
get
{
return false;
}
}
}
哥用来翻墙用的,简修改即可实现你的这个效果