我明天给你个代码 今天我们要开会 没时间啊
给你个例子
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebResponse.aspx.cs" Inherits="WebResponse" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div style="font-size: 12pxwidth:80%margin:0 autoheight: 32px">网址<asp:TextBox ID="TextBox1" runat="server" Width="339px"></asp:TextBox> <asp:Button ID="Button1"
runat="server" Text="获取" OnClick="Button1_Click" /></div>
<div style="width:80%margin:0 auto">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" Width="100%">
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="Url,Title" DataNavigateUrlFormatString="./HtmlFiles/{0}" DataTextField="Title" HeaderText="标题" />
<asp:BoundField DataField="BuildTime" HeaderText="创建时间" />
<asp:HyperLinkField DataNavigateUrlFields="LocalUrl" DataNavigateUrlFormatString="./HtmlFiles/{0}" DataTextField="LocalUrl" HeaderText="原始网址" />
</Columns>
</asp:GridView>
</div>
</div>
</form>
</body>
</html>
using System
using System.Data
using System.Configuration
using System.Collections
using System.Web
using System.Web.Security
using System.Web.UI
using System.Web.UI.WebControls
using System.Web.UI.WebControls.WebParts
using System.Web.UI.HtmlControls
using System.Net
using System.IO
public partial class WebResponse : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//初始化一个内存表用来存放已经抓取并生成的静态页面的相关信息
DataTable dt = new DataTable()
DataColumn dc
dc = new DataColumn("Title", typeof(string))//网页标题
dt.Columns.Add(dc)
dc = new DataColumn("Url", typeof(string))//原始路径
dt.Columns.Add(dc)
dc = new DataColumn("LocalUrl", typeof(string))//生成的静态页面的路径
dt.Columns.Add(dc)
Session["HtmlFiles"] = dt
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string url = TextBox1.Text.Trim()
string content = ""
if (url.Length >0)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url)
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse()
StreamReader sr = new StreamReader(webResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("gb2312"))
content = sr.ReadToEnd()
//提取页面标题,即html中<title>与</title>之间的文字
//注意在这个方法中StreamReader使用的Encoding, 如果不一致可能出现乱码
//我采用的是比较笨的方法,只用来说明问题
int s = content.LastIndexOf("<title>") + 7
int l = content.LastIndexOf("</title>") + 8
string title = ""
if (s >0 &l >0 &l >s)
{
title = content.Substring(s, l - s)
}
else
{
title = "UnKnown"
}
string htmlFileName = ""
if (content.Length >0)
{
htmlFileName = DateTime.Now.ToString("yyMMdd-hhmmss") + ".html"
System.IO.StreamWriter sw = new StreamWriter(Server.MapPath("./HtmlFiles/")+htmlFileName,false,System.Text.Encoding.GetEncoding("gb2312"))
try
{
sw.Write(content)
}
catch
{
throw (new Exception("程序执行出错"))
}
//将获取的页面信息
DataTable dt = (DataTable)Session["HtmlFiles"]
DataRow dr = dt.NewRow()
dr["Title"] = title
dr["Url"] = htmlFileName
dr["BuildTime"] = DateTime.Now.ToString()
dr["LocalUrl"] = url
dt.Rows.Add(dr)
GridViewDataBind()
string js = "<script>alert(\"生成成功!\")</script>"
this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Msg", js)
sw.Flush()
sw.Close()
sw.Dispose()
}
}
}
private void GridViewDataBind()
{
DataTable dt = (DataTable)Session["HtmlFiles"]
this.GridView1.DataSource = dt
this.GridView1.DataBind()
}
}
注意在根目录下建立HtmlFiles文件夹 希望对你能够有帮助
首先index.html这样就不行要改成index.asp你可以先你下那个是怎么连库的
然后再在你的index.asp中做相应的修改
但是如果你一点都不懂asp 我想也不是很容易的事
呵呵
其实这种事是很简单的
用心休会一下就可以了
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)