2015-02-10 52 views
0

任何人都可以闡明citebite如何實現緩存,特別是它如何能夠顯示與原始頁面具有相同佈局的緩存?cite bite如何實現緩存?

我期待實現了非常類似的東西:我使用

public static string sourceCache (string URL) 
{ 
     string sourceURL = URL; 
     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sourceURL); 
     HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 

     if (response.StatusCode == HttpStatusCode.OK) 
     { 
      Stream receiveStream = response.GetResponseStream(); 
      StreamReader readStream = null; 

      if (response.CharacterSet == null) 
      { 
       readStream = new StreamReader(receiveStream); 
      } 
      else 
      { 
       readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet)); 
      } 

      string data = readStream.ReadToEnd(); 

      response.Close(); 
      readStream.Close(); 

      return data; 
     } 

     return "couldn't retrieve cache"; 
    } 
} 

源,然後我發到我的數據庫存儲爲nvarchar(max)拉的HTML。當加載頁面以顯示緩存時,我將該字段拖動並將其設置爲div屬性的innerhtml。

但是,儘管它們的緩存保留了源頁面的樣式和佈局,但我並沒有。

我哪裏錯了?

我有一個asp.net 4.5 C#網絡形成網站

回答

0

創建一個該頁面,看看源。祕密是

<base href="http://stackoverflow.com/questions/28432505/how-does-cite-bite-acheive-its-cache" /> 

在HTML基礎元件()指定的基本URL以用於包含在一個document.There之內的所有 相對URL是最大一個文檔中的一個元素 。

+0

謝謝。實際上,我的網站適用於這個網頁,我猜是因爲它包含了這個基本元素。我查看了一個不適合我的網站的頁面源,但是在citebite中工作並且沒有基本元素,所以你是說我必須查找該基本元素,如果它不是那裏插入一個到我的「緩存」與HREF設置爲源網址,我緩存? – Dave 2015-02-10 13:35:52

0

根據基本元素上方的@Alex K似乎是問題所在。

我修改了代碼來檢查,如果現有的HTML有「基地HREF」在裏面,如果沒有插入帶有設置爲源URL

public static string sourceCache (string URL) 
    { 
     string sourceURL = URL; 
     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sourceURL); 
     HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 

     if (response.StatusCode == HttpStatusCode.OK) 
     { 
      Stream receiveStream = response.GetResponseStream(); 
      StreamReader readStream = null; 

      if (response.CharacterSet == null) 
      { 
       readStream = new StreamReader(receiveStream); 
      } 
      else 
      { 
       readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet)); 
      } 

      string data = readStream.ReadToEnd(); 

      response.Close(); 
      readStream.Close(); 

      if (data.Contains("base href")) 
      { 
       return data; 
      } 
      else 
      { 
       //we need to insert the base href with the source url 
       data = basecache(data, URL); 
       return data; 
      }     
     } 

     return "couldn't retrieve cache"; 
    } 

    public static string basecache (string htmlsource, string urlsource) 
    { 
     //make sure there is a head tag 
     if (htmlsource.IndexOf("<head>") != -1) 
     { 
      int headtag = htmlsource.IndexOf("<head>"); 
      string newhtml = htmlsource.Insert(headtag + "<head>".Length, "<base href='" + urlsource + "'/>"); 
      return newhtml; 
     } 
     else if(htmlsource.IndexOf("&lt;head&gt;") != -1) 
     { 
      int headtag = htmlsource.IndexOf("&lt;head&gt;"); 
      string newhtml = htmlsource.Insert(headtag + "&lt;head&gt;".Length, "<base href='" + urlsource + "'/>"); 
      return newhtml; 
     } 
     else 
     { 
      return htmlsource; 
     } 
    } 

到目前爲止我」在href基本元素我只測試了幾個網站/域名,但它似乎工作,非常感謝亞歷克斯的幫助。