[System.Net]模拟Web请求编写简易单词查询客户端
demo: 我就不上传了
前言
在实际生活中,网络请求的应用极其常见,比如使用浏览器,程序中我们还要调用webservice。那么浏览器是怎么请求网络资源的呢?不用它可以自己请求不?
答案是可以的。
如果我们可以用自己的程序主动发起网络请求,那么我们可以:模拟提交数据,做一些简单网页游戏的外挂,可以刷一些帖子的访问量,可以抓取网络上的资源……
废话不我说,此文以使用Get方式对有道词典网进行请求为核心,编写了一个简单的单词查询客户端。正则表达式不太懂可以改为字符串基本操作,或者对其自己做一下基本的了解和学习。
【注:致初学都系列,一般以简单有意义的demo为例,介绍某类知识点(此文已以黑体标出)。其中API不懂的可以留言讨论。如有兴趣可以查询MSDN进行深入学习(笔者所期望的)】
效果图:
代码:
usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Net;usingSystem.Text;usingSystem.Text.RegularExPRessions;namespaceNetCapture
{///<summary>///base类。以后我们可能写别的类似请求,通过继承此类,可以省下一些代码。///</summary>publicabstractclassGetScraperBase
{//正则表达式的匹配模式protectedabstractstringPattern {get; }//如何过滤正则表达式匹配的结果protectedabstractDictionary<string,string>FilterMatch(Match match);//抓取网页上的内容publicDictionary<string,string> Scrape(stringurl, WebProxy proxy =null)
{varrequest =WebRequest.Create(url); //创建requestif(proxy !=null)
{
request.Proxy= proxy;//可能你在一些环境上不了网,得使用代理服务器}
varresponse =request.GetResponse(); //获取responsevarstream =response.GetResponseStream();varresponseReader =newStreamReader(stream);varcontent =responseReader.ReadToEnd(); //读取response(页面对应的html)varmatch =Regex.Match(content, Pattern, RegexOptions.IgnorePatternWhitespace); //筛选内容returnFilterMatch(match);
}
}publicclassYouDaoScaper : GetScraperBase
{protectedoverridestringPattern
{get{/*Target result in response:
<div class="trans-container">
<ul>
<li>n. 试验;检验</li>
<li>vt. 试验;测试</li>
<li>vi. 试验;测试</li>
<li>n. (Test)人名;(英)特斯特</li>
</ul>
*
* there are two groups in this pattern, first is '<li>(?<content>.+)</li>[\r\n\s]*'
* it's an unnamed group, it has four capture:
* first is '<li>n. 试验;检验</li>' and so on.
*
* another group is and named group 'content' , it has four capture, in this sampe:
* capture 1 is 'n. 试验;检验' and so on.*/return@"<div\sclass=""trans-container"">[\r\n\s]*
<ul>[\r\n\s]*
(<li>(?<content>.+)</li>[\r\n\s]*)*
</ul>";
}
}protectedoverrideDictionary<string,string>FilterMatch(Match match)
{vardict=newDictionary<string,string>();varcontent ="";vargroup=match.Groups["content"];if(group.Success)
{foreach(Capture captureingroup.Captures)
{
content+= (capture.Value +"\n");
}
}
dict["content"]=content;returndict;
}publicstringQueryWord(stringword)
{varurl="http://dict.youdao.com/search?q="+word;vardict =Scrape(url);returndict["content"];
}
}
}