[For beginners] mock Web request--get

Source: Internet
Author: User
Tags string indexof

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> &LT;LI&GT;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.