System.IO.IOException问题,请教高手
薏小米 2013-06-14 08:17:57 我想实现抓取网页的某条信息,点击按钮的click事件,先把网页的源代码显示在textbox2里,然后再在里边查找,将查找结果显示在textbox6中,但每次运行到textbox6.text,程序就会出错,textbox6中就没有任何输出,
出错信息为:未处理的“System.IO.IOException”类型的异常出现在 mscorlib.dll 中。
哪位大神可以指点一下哈~!非常感谢!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
namespace a
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
HttpWebRequest request;// HttpWebRequest是类名;
HttpWebResponse response;
TextReader tr;
string url = textBox1.Text.ToString();//强制类型转换
request = (HttpWebRequest)WebRequest.Create(url);//生成去访问网页的指令
response = (HttpWebResponse)request.GetResponse();//request.GetResponse()类的成员函数,访问的返回指令;
tr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(936));//分配空间
textBox2.Text = tr.ReadToEnd();
response.Close();
WebExpress(textBox2.Text.ToString());//输出返回值;
}
private void WebExpress(string s)
{
string tag5 = "<h1>";
string tag6 = "<span class=\"subtitle\">";
int pos5, pos6;
pos5 = s.IndexOf(tag5);
textBox5.Text = pos5.ToString();
while (pos5 >0)
{
pos6 = s.IndexOf(tag6, pos5 + 1);
if (pos6 < 0)
return;
string url3 = s.Substring(pos5 + tag5.Length, pos6 - pos5 - tag5.Length);
textBox6.Text = url3 + "\n";
s = s.Substring(pos6 + 1);//循环
pos5 = s.IndexOf(tag5);//
}
}
}
}