• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#破解防盗链

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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);


        }


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#NPOI使用发布时间:2022-07-10
下一篇:
C#类中无参/有参构造函数的使用例子1发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap