2015-02-10 71 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基本元素我只测试了几个网站/域名,但它似乎工作,非常感谢亚历克斯的帮助。