首页 > 学院 > 开发设计 > 正文

获取目标网页的内容

2019-11-17 01:37:27
字体:
来源:转载
供稿:网友

获取目标网页的内容

 1             HttpWebRequest request = WebRequest.Create("http://www.baidu.com/") as HttpWebRequest; 2             HttpWebResponse response = request.GetResponse() as HttpWebResponse; 3  4             if (response.StatusCode == HttpStatusCode.OK) 5             { 6                 string characterSet = response.CharacterSet; 7                 Encoding encode; 8                 if (characterSet != "") 9                 {10                     encode = Encoding.GetEncoding(characterSet);11                 }12                 else13                 {14                     encode = Encoding.Default;15                 }16 17                 Stream receiveStream = response.GetResponseStream();18                 StreamReader reader = new StreamReader(receiveStream, encode);19                 char[] read = new char[255];20                 // 每次读取25521                 int count = reader.Read(read, 0, 255);22                 string str = string.Empty;23                 while (count > 0)24                 { // 如果读到一定数目的字符串后,将这些字符串输入到string中25                     str += new String(read, 0, count);26                     count = reader.Read(read, 0, 255); // 再读下面的255内容27                 }28                 webBrowser1.DocumentText = str;29 30                 // 读取完毕31                 reader.Close();32                 receiveStream.Close();


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表