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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值