Internet 网络资源非常丰富,几乎涉及到日常生活和研究的各个方面。流行的搜索引擎像Google、百度、雅虎等都能完成快速搜索网络资源的功能。本节我们将学习用C#实现这些功能的基本思路。

  在System.Net 命名空间中,有一个WebClient 类,该类提供向URI 标识的任何本地、Intranet或Internet 资源发送数据以及从这些资源接收数据的公共方法。

  URI 的意思是统一资源标识符,.NET 框架使用URI 来标识所请求的Internet 资源和通信协议。URI 由四部分组成:协议标识符、服务器标识符、路径标识符和可选的查询字符串。其中协议标识符标识用于请求和响应的通信协议;服务器标识符由域名系统(DNS)主机名或TCP 地址组成,用于惟一标识Internet 上的服务器;路径标识符用于在服务器上定位请求的信息;查询字符串用于将信息从客户端传送到服务器。

  例如,“http://www.contoso.com/whatsnew.aspx?date=today”就是由协议标识符“http”、服务器标识符“www.contoso.com”、路径“whatsnew.aspx”和查询字符串“?date=today”组成的。服务器接收到请求并对响应进行了处理之后,就将该响应返回到客户端应用程序。由于响应中包括了像原始文本或XML 数据等相关信息,因此我们就可以从这些信息中查询需要的文本字符串、图片等内容。

  通过WebClient 类从Web 站点下载文件有两种方式,一种是直接保存为本地文件,另一种是通过流进行读取,具体采用哪种方式要视情况而定。

  如果要保存成本地文件,可以使用DownloadFile 方法。该方法有两个参数,一个是URI,即统一资源标识符;另一个是本地保存路径。例如:

using System.Net;
……
WebClient myWebClient = new WebClient();
myWebClient.DownloadFile("http://military.china.com/zh_cn/", "C:\\test.htm");

  如果应用程序需要处理从Web 站点检索到的数据,可以使用OpenRead 方法,这个方法返回一个Stream 引用。然后就可以从数据流中读取数据了。例如:

using System.Net;
using System.IO;
……
WebClient webClient = new WebClient();
Stream myStream = webClient.OpenRead("http://news.sohu.com");
StreamReader sr = new StreamReader(myStream);
string httpSource = sr.ReadToEnd();

  也可以使用DownloadString 方法,直接读取包含网页源代码的字符串内容。例如:

using System.Net;
……
WebClient webClient = new WebClient();
string httpSource = webClient.DownloadString("http://news.sohu.com");

  这种方法得到的结果与使用流读取得到的结果相同。

  【例】设计一个简单的搜索程序,能读取指定网址的源文件,并能用正则表达式搜索包含的超链接与图片。

  (1) 新建一个名为InternetSearchExample 的Windows 应用程序,修改Form1.cs 为FormSearch.cs,设计如图7-4 所示的界面。

  

  (2) 切换到代码方式,添加对应的命名空间引用和事件,源代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//添加的命名空间引用
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
namespace InternetSearchExample
{
public partial class FormSearch : Form
{
public FormSearch()
{
InitializeComponent();
}
private void buttonSearch_Click(object sender, EventArgs e)
{
listBoxLinks.Items.Clear();
listBoxImage.Items.Clear();
pictureBox1.Image = null;
webBrowser1.Url = new Uri("about:blank");
richTextBox1.Clear();
string urlString = textBoxUrl.Text.Trim();
if (urlString.StartsWith("http://") == false)
{
urlString = "http://" + urlString;
textBoxUrl.Text = urlString;
}
string httpSource;
try
{
//设置鼠标形状为沙漏形状
Cursor.Current = Cursors.WaitCursor;
WebClient webClient = new WebClient();
//获取包含网页源代码的字符串。
httpSource = webClient.DownloadString(textBoxUrl.Text);
richTextBox1.Text = httpSource;
webBrowser1.Url = new Uri(textBoxUrl.Text);
}
catch (Exception err)
{
MessageBox.Show(err.Message);
return;
}
finally
{
//设置鼠标形状为默认形状
Cursor.Current = Cursors.Default;
}
string regexHrefPattern = @"<a\s+href\s*=\s*""?([^"" >]+)""?>(.+)</a>";
Regex myRegex = new Regex(regexHrefPattern, RegexOptions.IgnoreCase);
Match myMatch = myRegex.Match(httpSource);
while (myMatch.Success == true)
{
listBoxLinks.Items.Add(myMatch.Groups[0].Value);
myMatch = myMatch.NextMatch();
}
string regexImgPattern =
@"<img[^>]+(src)\s*=\s*""?([^ "">]+)""?(?:[^>]+([^"">]+)""?)?";
myRegex = new Regex(regexImgPattern, RegexOptions.IgnoreCase);
myMatch = myRegex.Match(httpSource);
while (myMatch.Success == true)
{
listBoxImage.Items.Add(myMatch.Groups[2].Value);
myMatch = myMatch.NextMatch();
}
}
private void listBoxImage_Click(object sender, EventArgs e)
{
try
{
WebClient client = new WebClient();
pictureBox1.Image =
Image.FromStream(client.OpenRead(listBoxImage.SelectedItem.ToString()));
}
catch
{
pictureBox1.Image = null;
}
}
}
}

  (3) 按<F5>键编译并运行,输入某个网址,例如http://news.sohu.com,然后单击【开始搜索】按钮,观察搜索结果。单击搜索结果2 中的图片链接,即可显示对应的图片。运行效果如图所示。

  当然,实际应用中的搜索引擎远比这个例子复杂得多。但是,通过这个例子,我们学习了利用C#和正则表达式编写搜索网络资源程序的基本思想。在此基础上,编写与此相关的更广泛的网络应用程序就不会感到太困难了。

 

最新文章

  1. [转]教你一招 - 如何给nopcommerce增加一个类似admin的area
  2. WampServer 的phpmyadmin数据
  3. Super A^B mod C
  4. Eclipse 调试 Java 程序的技巧
  5. substr
  6. VMware虚拟机中的常用文件介绍
  7. windws 安装jdk
  8. 254. Factor Combinations
  9. 转载Sql 获取数据库所有表及其字段名称,类型,长度
  10. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(33)-数据验证共享
  11. 安装oracle客户端(navicat for oracle)
  12. UCI
  13. BZOj 4540: [Hnoi2016]序列 [莫队 st表 预处理]
  14. Beta No.6
  15. .htaccess 文件来进行用户组的目录权限访问控制
  16. Oracle 服务器运行健康状况监控利器 Spotlight on Oracle 的安装与使用
  17. centos7编译安装lnmp
  18. [CQOI2015]任务查询系统(未完成)
  19. [转]POJ3624 Charm Bracelet(典型01背包问题)
  20. hdu2064

热门文章

  1. vmstat参数详解
  2. 【Linux】cp命令的各种妙用
  3. 利用Numpy求解投资内部收益率IRR
  4. windows下的:开始→运行→命令
  5. B树的进化版----B+树
  6. Docker数据目录迁移解决方案
  7. Vue 标签Style 动态三元判断绑定
  8. VMware中安装Ubuntu后,安装VMwareTools提示“Not enough free space to extract VMwareTools-10.3.10-13959562.tar.gz”的解决办法
  9. pycharm安装完成后的一些基本设置
  10. (转载)微软数据挖掘算法:Microsoft Naive Bayes 算法(3)