C# 破解防盗链
C#版本 java類似 流用法略不同
private void WriteFile(byte[] info, string fullpath)
??????? {
??????????? FileStream fs = null;
??????????? BinaryWriter w = null;
??????????? try
??????????? {
??????????????? //string name = DateTime.Now.ToString("yyyy-MM-dd HH.mm.ss.ffffff") + ".html";
??????????????? //string path = Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, "MN\\" + name);
??????????????? fs = new FileStream(fullpath, FileMode.CreateNew, FileAccess.ReadWrite);
??????????????? // 轉換為字節 寫入數據 ( 可寫入中文 )
??????????????? //Byte[] info = Encoding.GetEncoding("GB2312").GetBytes(content);
??????????????? // 字節數組 , 字節偏移量 , 最多寫入的字節數
??????????????? w = new BinaryWriter(fs);
??????????????? // 設置要寫入的偏移量 ?
??????????????? fs.Position = fs.Length;
??????????????? fs.Write(info, 0, info.Length);
??????????? }
??????????? finally
??????????? {
??????????????? if (w != null)
??????????????????? w.Close();
??????????????? if (fs != null)
??????????????????? fs.Close();
??????????? }
??????? }
?private void imgService(HttpContext context)
??????? {
??????????? HttpRequest request = context.Request;
??????????? HttpResponse response = context.Response;
??????????? string url = request.QueryString["url"];
??????????? string fileName = url.GetHashCode().ToString()+".gif";
??????????? string ipaddress = System.Configuration.ConfigurationManager.AppSettings["ipaddress"];
??????????? //string path = System.Configuration.ConfigurationManager.AppSettings["path"];
??????????? string savePath = URLUtil.getDomainName(url).Replace(".", "_");//保存的文件夾
??????????? //string fileName = url.Substring(url.LastIndexOf("/")+1);//保存的文件名
??????????? string savename = context.Server.MapPath("imgs") + "\\" + savePath + "\\" + fileName;
??????????? if (!File.Exists(savename))
??????????? {
??????????????? HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
??????????????? webrequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/QVOD, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
??????????????? webrequest.Headers.Add("Accept-Encoding", "gzip, deflate");
??????????????? webrequest.Headers.Add("Accept-Language", "zh-cn");
??????????????? webrequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; MS-RTC LM 8; Alexa Toolbar)";
??????????????? webrequest.Headers.Add("UA-CPU", "x86");
??????????????? webrequest.KeepAlive = true;
??????????????? HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
???????????????
??????????????? if (webresponse.StatusCode == HttpStatusCode.OK)
??????????????? {
?????????????????? // System.Drawing.Image image = System.Drawing.Image.FromStream(webresponse.GetResponseStream());
??????????????????? Stream st = webresponse.GetResponseStream();
??????????????????? BinaryReader br = new BinaryReader(st);
??????????????????? byte[] poto = br.ReadBytes(4096+(int)webresponse.ContentLength);
??????????????????? if (!System.IO.Directory.Exists(context.Server.MapPath("imgs") + "\\" + savePath))
??????????????????? {
??????????????????????? System.IO.Directory.CreateDirectory(context.Server.MapPath("imgs") + "\\" + savePath);
??????????????????? }
??????????????????? WriteFile(poto, savename);
???????????????????
??????????????????? /*string ct=webresponse.ContentType.ToLower();
??????????????????? if (ct.IndexOf("gif") > 0)
??????????????????? {
??????????????????????? //image.Save(savename);
??????????????????? }
??????????????????? else
??????????????????? {
?????????????????????? //image.Save(savename, System.Drawing.Imaging.ImageFormat.Gif);//保存路徑
??????????????????? }
??????????????????? //image.Dispose();//釋放資源 */
???????????????
??????????????? }
??????????? }
??????????? response.Redirect(ipaddress +savePath+"/"+ fileName);
??????? }
轉載于:https://www.cnblogs.com/MakethingsEasy/archive/2012/08/03/2622165.html
總結
- 上一篇: PowerDesigner表结构和字段大
- 下一篇: 并查集小结(转)