新闻系统(3)内容保护的探索
這些年,互聯(lián)網(wǎng)垃圾站已經(jīng)成為一個非常大規(guī)模的產(chǎn)業(yè),所謂天下文章一大抄,特別是在版權(quán)意識不強(qiáng)的中國,這個現(xiàn)象尤為嚴(yán)重,當(dāng)一個網(wǎng)站辛苦整理的資料被人瘋狂轉(zhuǎn)載的時(shí)候,原創(chuàng)的網(wǎng)站可能都會被當(dāng)成是垃圾網(wǎng)站了。在以往,搜索引擎不是很喜歡論壇和博客的資料,也許以為灌水過多,價(jià)值不大,可是現(xiàn)在發(fā)現(xiàn)搜索引擎非常青睞博客和論壇這些原創(chuàng)資料。
??我們總是希望搜索引擎多收錄我們的資料,以便提高流量,所謂seo,可是反過來,當(dāng)我們被搜索引擎抓取的時(shí)候,也是非常適合那些垃圾站的抓取。這可是有點(diǎn)兩難。我們看到很多網(wǎng)站都開始轉(zhuǎn)向偏向于寧可少被百度收錄,也要保護(hù)自己的版權(quán)。
??常見的辦法是把內(nèi)容做成圖片,把文字做成圖片的軟件還是比較多,我們就不贅述了,我們主要討論下能不能用程序來解決這個問題。
??Html要繪制成圖片,首先得做html的解析,這個是很有難度的,相當(dāng)于你得做一個瀏覽器的內(nèi)核,那么最簡單的辦法莫過于直接使用ie的瀏覽器。
??在net中,我們知道WebBrowser。那么我們就可以依托這個,來做一個抓屏的效果,我們首先讓html在一個web地址上呈現(xiàn),再通過我們的WebBrowser去讀取這個頁面,然后把內(nèi)容生成圖片。
??代碼不多,請看代碼。首先聲明以下很多代碼轉(zhuǎn)自網(wǎng)絡(luò),我只做了部分?jǐn)U展。
??using System;
??using System.Drawing;
??using System.Drawing.Imaging;
??using System.Windows.Forms;
??using mshtml;
??namespace webabc
??{
??public class HtmlToImage
??{
??int S_Height;
??int S_Width;
??int F_Height;
??int F_Width;
??string MyURL;
??public int ScreenHeight
??{
??get
??{
??return S_Height;
??}
??set
??{
??S_Height = value;
??}
??}
??public int ScreenWidth
??{
??get
??{
??return S_Width;
??}
??set
??{
??S_Width = value;
??}
??}
??public int ImageHeight
??{
??get
??{
??return F_Height;
??}
??set
??{
??F_Height = value;
??}
??}
??public int ImageWidth
??{
??get
??{
??return F_Width;
??}
??set
??{
??F_Width = value;
??}
??}
??public string WebSite
??{
??get
??{
??return MyURL;
??}
??set
??{
??MyURL = value;
??}
??}
??public HtmlToImage(string WebSite, int ScreenWidth, int ScreenHeight, int ImageWidth, int ImageHeight)
??{
??this.WebSite = WebSite;
??this.ScreenHeight = ScreenHeight;
??this.ScreenWidth = ScreenWidth;
??this.ImageHeight = ImageHeight;
??this.ImageWidth = ImageWidth;
??}
??public Bitmap GetBitmap()
??{
??WebPageBitmap Shot = new WebPageBitmap(this.WebSite, this.ScreenWidth, this.ScreenHeight);
??Shot.GetIt();
??Bitmap Pic = Shot.DrawBitmap(this.ImageHeight, this.ImageWidth);
??return Pic;
??}
??}
??public class WebPageBitmap
??{
??WebBrowser MyBrowser;
??string URL;
??int Height;
??int Width;
??public WebPageBitmap(string url, int width, int height)
??{
??this.URL = url;
??this.Width = width;
??this.Height = height;
??MyBrowser = new WebBrowser();
??//if (System.Web.HttpContext.Current.Cache["dd"] == null)
??//{
??// System.Web.HttpContext.Current.Cache["dd"]=MyBrowser ;
??//}
??//else
??//{
??// MyBrowser = (WebBrowser)System.Web.HttpContext.Current.Cache["dd"];
??//}
??MyBrowser.ScrollBarsEnabled = false;
??MyBrowser.Size = new Size(this.Width, this.Height);
??}
??public void GetIt()
??{
??MyBrowser.Navigate(this.URL);
??while (MyBrowser.ReadyState != WebBrowserReadyState.Complete)
??{
??Application.DoEvents();
??}
??IHTMLDocument2 doc2 = (IHTMLDocument2)MyBrowser.Document.DomDocument;
??IHTMLDocument3 doc3 = (IHTMLDocument3)MyBrowser.Document.DomDocument;
??IHTMLElement2 body2 = (IHTMLElement2)doc2.body; //doc2.body;
??IHTMLElement2 root2 = (IHTMLElement2)doc3.documentElement;//doc3.documentElement;
??// Determine dimensions for the image; we could add minWidth here
??// to ensure that we get closer to the minimal width (the width
??// computed might be a few pixels less than what we want).
??int __width = Math.Max(body2.scrollWidth, root2.scrollWidth);
??int __height = Math.Max(root2.scrollHeight, body2.scrollHeight);
??this.Height = __height;
??this.Width = __width;
??MyBrowser.Size = new Size(__width, __height);
??}
??public Bitmap DrawBitmap(int theight, int twidth)
??{
??Bitmap myBitmap = new Bitmap(this.Width, this.Height);
??Rectangle DrawRect = new Rectangle(0, 0, this.Width, this.Height);
??MyBrowser.DrawToBitmap(myBitmap, DrawRect);
??System.Drawing.Image imgOutput = myBitmap;
??System.Drawing.Bitmap oThumbNail = new Bitmap(this.Width, this.Height, imgOutput.PixelFormat);
??Graphics g = Graphics.FromImage(oThumbNail);
??//g.Clear(Color.Transparent);
??g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
??g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
??g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
??Rectangle oRectangle = new Rectangle(0, 0, this.Width, this.Height);
??ImageAttributes attr = new ImageAttributes();
??//attr.SetColorKey(Color.White,Color.White);
??g.DrawImage(imgOutput, oRectangle, 0, 0, imgOutput.Width, imgOutput.Height, GraphicsUnit.Pixel, attr);
??try
??{
??return oThumbNail;
??}
??catch
??{
??return null;
??}
??finally
??{
??imgOutput.Dispose();
??imgOutput = null;
??MyBrowser.Dispose();
??MyBrowser = null;
??}
??}
??}
??}
??一些深刻技術(shù)分析的文章在博客園也又不少分析,請搜索下他們的代碼。我們要說明的幾點(diǎn)是。
??IHTMLDocument2 doc2 = (IHTMLDocument2)MyBrowser.Document.DomDocument;
??IHTMLDocument3 doc3 = (IHTMLDocument3)MyBrowser.Document.DomDocument;
??IHTMLElement2 body2 = (IHTMLElement2)doc2.body; //doc2.body;
??IHTMLElement2 root2 = (IHTMLElement2)doc3.documentElement;//doc3.documentElement;
??// Determine dimensions for the image; we could add minWidth here
??// to ensure that we get closer to the minimal width (the width
??// computed might be a few pixels less than what we want).
??int __width = Math.Max(body2.scrollWidth, root2.scrollWidth);
??int __height = Math.Max(root2.scrollHeight, body2.scrollHeight);
??this.Height = __height;
??this.Width = __width;
??MyBrowser.Size = new Size(__width, __height);
??這一部分,我們通過對內(nèi)容的分析,可以得出文檔的高,而不是顯示器一屏的高。在這里我們還可以擴(kuò)展一下,直接插入html讓W(xué)ebBrowser進(jìn)行繪制,而不用去瀏覽某個網(wǎng)址。大家可以嘗試一下。
??web調(diào)用的代碼如下,要注意這個: newThread.SetApartmentState(ApartmentState.STA);
??using System;
??using System.Collections.Generic;
??using System.Drawing.Imaging;
??using System.Web;
??using System.Threading;
??/// <summary>
??///My_html_to_img 的摘要說明
??/// </summary>
??public class My_html_to_img
??{
??public My_html_to_img()
??{
??//
??//TODO: 在此處添加構(gòu)造函數(shù)邏輯
??//
??}
??public string nid="";
??public string oinfo = "E:";
??public string url = string.Empty;
??public string path = "";
??public string NewsContentToImages()
??{
??//dd();
??try
??{
??url = "http://www.21nm.net/NewsContentToImages.aspx?id=" + nid;
??//path = System.Web.HttpContext.Current.Server.MapPath("../uploads/newscontentimages/") + nid + ".gif";
??Thread newThread = new Thread(new ThreadStart(dd));
??newThread.Name = "a88";
??newThread.SetApartmentState(ApartmentState.STA);
??newThread.Start();
??oinfo = "1";
??}
??catch (Exception ex)
??{
??oinfo=ex.ToString();
??}
??return oinfo;
??}
??void dd()
??{
??try
??{
??webabc.HtmlToImage thumb = new webabc.HtmlToImage(url, 1024, 768, 320, 240);
??System.Drawing.Bitmap x = thumb.GetBitmap();
??x.Save(path, ImageFormat.Gif);
??//Response.ContentType = "image/gif";
??oinfo += "ok";
??//oinfo += "{ " + System.Web.HttpContext.Current.Server.MapPath("../uploads/newscontentimages") + Request.QueryString["id"] + ".gif";
??}
??catch (Exception ex)
??{
??oinfo += ex.ToString();// +url + "{ " + System.Web.HttpContext.Current.Server.MapPath("../uploads/newscontentimages") + Request.QueryString["id"] + ".gif";
??//Response.Write(ex.ToString());
??}
??}
??}
??另外啰嗦一點(diǎn)就是,千萬別在web的多線程中用web的對象,貌似使用后,vs不報(bào)錯,就給一個類似死循環(huán)癥狀,要糾錯很麻煩。
??一個實(shí)際中使用的地址:http://www.21nm.net/html/c61/267230p1.html
??這個程序也有一個毛病,就是由于ie內(nèi)核的繪制,還是比較消耗資源的,不適合實(shí)時(shí)生成,最好是在添加新聞的時(shí)候一次生成圖片。或者在服務(wù)器資源消耗不高的時(shí)候批量生成。
??圖片的生成還有一個辦法,如果文章中的主要是文字,不會有表格,圖片什么的,我們可以直接用繪制圖片,這樣速度比較快,弊端是程序會比較復(fù)雜,工作量比較大,需要解析換行,加粗,什么的,要想顯示圖片或者表格這些就更難了。但是在做小說網(wǎng)這樣內(nèi)容那個,幾乎不可能有圖片的還是非常可行的。
??另外對于文章內(nèi)容加密,我想到的一個辦法,也嘗試過的是,把內(nèi)容des加密,用flash客戶端來解密呈現(xiàn)。理論上應(yīng)該是沒問題,可是實(shí)施起來卻遇到一個問題,沒有現(xiàn)成的as的des解密能和net的通用,主要是補(bǔ)全的模式不一樣,得自己寫一個雙方能通用的算法。這個也是比較費(fèi)時(shí)間的。
??如果哪位高手能找到或者寫出了以上兩種的解決方案,或者有其他更好的解決方案,還請賜教吧。
??就文章內(nèi)容是否值得加密,就不用討論了,就算不值得,可只要客戶需要,我們也得做啊。
總結(jié)
以上是生活随笔為你收集整理的新闻系统(3)内容保护的探索的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 蓝丰生化是做什么的
- 下一篇: 京东e卡必须一次用完吗