In real life, the application of network requests is very common, such as using a browser, in the program we also call WebService. So how does the browser request network resources? You don't have to ask for it, do you?
The answer is yes.
If we can use their own programs to initiate network requests, then we can: simulate the submission of data, do some simple web game plug-in, you can brush some of the posts of the traffic, you can crawl resources on the network ...
Crap, no, I said. This paper uses get method to make the request of Youdao Dictionary network as the core, and writes a simple word query client. Regular expressions don't quite understand that you can change to a string indexof operation, or do basic understanding and learning about it yourself.
:
Code:
UsingSystem;UsingSystem.Collections.Generic;UsingSystem.Io;UsingSystem.Net;UsingSystem.Text;UsingSystem.Text.RegularExpressions;NamespaceNetcapture{ <summary> Base class. We may write other similar requests later, and by inheriting this class, we can save some code. </summary> Public Abstract Class Getscraperbase { The matching pattern of regular expressions Protected Abstract String Pattern {Get; } How to filter the results of a regular expression match Protected Abstract Dictionary<String, String> Filtermatch(MatchMatch); Crawl content on a Web page Public Dictionary<String, String> Scrape(StringUrl, WebProxyProxy= Null) { VarRequest= WebRequest.Create(Url); If (Proxy!= Null) {Request.Proxy =Proxy;You may not be able to use a proxy server in some environments. } VarResponse=Request.GetResponse(); VarStream=Response.GetResponseStream(); VarResponsereader= New StreamReader(Stream); VarContent=Responsereader.ReadToEnd(); VarMatch= Regex.Match(Content, Pattern, RegexOptions. Ignorepatternwhitespace); Return Filtermatch (Match); }} public class Youdaoscaper:getscraperbase {protected override string Pattern {get {/* Target result in response: & Lt;div class= "Trans-container" > <ul> <li>n. Test, inspection </li> <LI>VT. Test </li> <li>vi. Testing </li> <li>n. (test) name Coral Sea </li> </ul> * * There is the groups in this pattern, first is ' & Lt;li> (<content>.+) </li>[\r\n\s]* ' s an unnamed group, it had four capture: * First is ' <li>n. Test </li> ' and so on. * * Another group is and named group ' content ', it had four captures, in this sampe: * Capture 1 is ' N. test; test ' and so on. */return @ "<div\sclass=" "Trans-container" ">[\r\n\s]*<ul>[\r\n\s]* (<li> (? <content>.+) </li>[\r\n\s]*) *</ul> "; }} protected override Dictionary<string, String> Filtermatch (match match) {var dict=new dictionary<string, Stri Ng> (); var content = ""; var Group=matCh. groups["Content"]; if (group. Success) {foreach (Capture capture in group). Captures) {content + = (capture. Value + "\ n"); }} dict["Content"]=content; return dict; The public string Queryword (string word) {var url= "http://dict.youdao.com/search?q=" +word; var dict = Scrape (URL); return dict["Content"]; } }}
[For beginners] mock Web request--get