问题
我是用的WebClient类来获取网页源文件--在源文件里面查找是否存在关键字--存在就把这个源文件的连接存到数据库可是执行一段时间后 提示出现 基础连接已经关闭 连接被意外关闭 怎么解决?我自己也去网上搜了。说添加
webclient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");就可以解决。我加了还是解决不了--我的是windows程序以下是代码 /// <summary>
/// 提取网页源文件
/// </summary>
/// <param name="strlianjie">要提取源文件的网页</param>
public string TiQuYuanMa(string strlianjie)
{
WebClient webclient = new WebClient();
webclient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
Stream stream = webclient.OpenRead(strlianjie);
StreamReader streamreader = new StreamReader(stream, Encoding.Default);
strlianjie = streamreader.ReadToEnd();
return strlianjie;
} public void ChuLiWenZhangLieBiaoFenYe()
{
int i = caozuo.geshu("select count(*) from tb_wenzhangliebiaoshouye id"); //查询文章列表首页的数据个数
for (int x = 1; x <= i; x++)
{
for (int j = 0; j < 100; j++) //循环添加文章列表分页
{
string LieBiaoFenYe = null; //列表分页连接
string neirong = null; //存放文章列表分页的源文件
LieBiaoFenYe = caozuo.LieBiaoShouYe("select * from tb_wenzhangliebiaoshouye where id=" + x); //读取文章列表首页连接
if (LieBiaoFenYe != "") //判断列表分页连接是否为空
{
LieBiaoFenYe = LieBiaoFenYe + "/index/" + j;
neirong = TiQuYuanMa(LieBiaoFenYe); //提取文章列表分页的源文件
if (neirong.IndexOf("最近有点懒,还没有来得及写文章呢。", 0) == -1)
{
//判断是否有重复的数据
int chaxun = caozuo.geshu("select * from tb_wenzhangliebiaofenye where wenzhangliebiaofenye='" + LieBiaoFenYe + "'");
if (chaxun == 0)
{
//MessageBox.Show(LieBiaoFenYe); //测试用滴
//没有重复就写入数据库
caozuo.getcmd("insert tb_wenzhangliebiaofenye(wenzhangliebiaofenye) values(" + "\'" + LieBiaoFenYe + "\'" + ")");
}
}
else
{
break;
}
}
}
}
}
谁帮我想下解决办法好吗?
我都搞了1天了还是不知道怎么解决。。
感激各位高手了
我是用的WebClient类来获取网页源文件--在源文件里面查找是否存在关键字--存在就把这个源文件的连接存到数据库可是执行一段时间后 提示出现 基础连接已经关闭 连接被意外关闭 怎么解决?我自己也去网上搜了。说添加
webclient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");就可以解决。我加了还是解决不了--我的是windows程序以下是代码 /// <summary>
/// 提取网页源文件
/// </summary>
/// <param name="strlianjie">要提取源文件的网页</param>
public string TiQuYuanMa(string strlianjie)
{
WebClient webclient = new WebClient();
webclient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
Stream stream = webclient.OpenRead(strlianjie);
StreamReader streamreader = new StreamReader(stream, Encoding.Default);
strlianjie = streamreader.ReadToEnd();
return strlianjie;
} public void ChuLiWenZhangLieBiaoFenYe()
{
int i = caozuo.geshu("select count(*) from tb_wenzhangliebiaoshouye id"); //查询文章列表首页的数据个数
for (int x = 1; x <= i; x++)
{
for (int j = 0; j < 100; j++) //循环添加文章列表分页
{
string LieBiaoFenYe = null; //列表分页连接
string neirong = null; //存放文章列表分页的源文件
LieBiaoFenYe = caozuo.LieBiaoShouYe("select * from tb_wenzhangliebiaoshouye where id=" + x); //读取文章列表首页连接
if (LieBiaoFenYe != "") //判断列表分页连接是否为空
{
LieBiaoFenYe = LieBiaoFenYe + "/index/" + j;
neirong = TiQuYuanMa(LieBiaoFenYe); //提取文章列表分页的源文件
if (neirong.IndexOf("最近有点懒,还没有来得及写文章呢。", 0) == -1)
{
//判断是否有重复的数据
int chaxun = caozuo.geshu("select * from tb_wenzhangliebiaofenye where wenzhangliebiaofenye='" + LieBiaoFenYe + "'");
if (chaxun == 0)
{
//MessageBox.Show(LieBiaoFenYe); //测试用滴
//没有重复就写入数据库
caozuo.getcmd("insert tb_wenzhangliebiaofenye(wenzhangliebiaofenye) values(" + "\'" + LieBiaoFenYe + "\'" + ")");
}
}
else
{
break;
}
}
}
}
}
谁帮我想下解决办法好吗?
我都搞了1天了还是不知道怎么解决。。
感激各位高手了
XML code
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing="true"/>
</settings>
</system.net>
参考
我找不到application.config文件啊!我的是 windows应用程序。。
/// 通过URL取网页源代码
/// </summary>
/// <param name="url">URL</param>
/// <param name="encoding">网页编码</param>
/// <returns></returns>
private string GetHtmlCode(string url, Encoding encoding)
{
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
System.Net.WebResponse response = request.GetResponse();
System.IO.Stream resStream = response.GetResponseStream();
System.IO.StreamReader sr = new System.IO.StreamReader(resStream, encoding);
string html = (sr.ReadToEnd());
resStream.Close();
sr.Close();
return html;
}