任何人都可以了解citebite如何实现它的缓存,特别是它如何能够显示与原始页面具有相同布局的缓存?
我希望实现非常相似的东西:我使用
从源代码中提取htmlpublic 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)
的数据库。当加载页面以显示缓存时,我拉出字段并将其设置为div属性的innerhtml。
然而,在citebite上,他们的缓存保留了源页面的样式和布局,而我的缓存却没有。
我哪里错了?
我有一个asp.net 4.5 c #Web表单网站
答案 0 :(得分:0)
为此页面创建一个,查看源代码。秘密是
<base href="http://stackoverflow.com/questions/28432505/how-does-cite-bite-acheive-its-cache" />
HTML Base Element()指定用于所有人的基本URL 文档中包含的相对URL。最多只有一个 文档中的元素。
答案 1 :(得分:0)
根据上面的@Alex K,基本元素似乎是个问题。
我修改了代码以检查现有的html是否有&#34; base href&#34;在其中,如果没有插入基础元素,并将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("<head>") != -1)
{
int headtag = htmlsource.IndexOf("<head>");
string newhtml = htmlsource.Insert(headtag + "<head>".Length, "<base href='" + urlsource + "'/>");
return newhtml;
}
else
{
return htmlsource;
}
}
到目前为止,我只是在一些网站/域名上进行了测试,但它似乎有效,非常感谢Alex的帮助。