• [System.Net]模拟Web请求编写简易单词查询客户端


    demo: 我就不上传了

     

    前言

    在实际生活中,网络请求的应用极其常见,比如使用浏览器,程序中我们还要调用webservice。那么浏览器是怎么请求网络资源的呢?不用它可以自己请求不?

    答案是可以的。

    如果我们可以用自己的程序主动发起网络请求,那么我们可以:模拟提交数据,做一些简单网页游戏的外挂,可以刷一些帖子的访问量,可以抓取网络上的资源……

    废话不我说,此文以使用Get方式对有道词典网进行请求为核心,编写了一个简单的单词查询客户端。正则表达式不太懂可以改为字符串基本操作,或者对其自己做一下基本的了解和学习。

     【注:致初学都系列,一般以简单有意义的demo为例,介绍某类知识点(此文已以黑体标出)。其中API不懂的可以留言讨论。如有兴趣可以查询MSDN进行深入学习(笔者所期望的)】

     

    效果图:

     

     

    代码:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Net;
    using System.Text;
    using System.Text.RegularExpressions;
    
    namespace NetCapture
    {
        /// <summary>
        /// base类。以后我们可能写别的类似请求,通过继承此类,可以省下一些代码。
        /// </summary>
        public abstract class GetScraperBase
        {
            //正则表达式的匹配模式
            protected abstract string Pattern { get; }
            //如何过滤正则表达式匹配的结果
            protected abstract Dictionary<string, string> FilterMatch(Match match);
            //抓取网页上的内容
            public Dictionary<string, string> Scrape(string url, WebProxy proxy = null)
            {
                    var request = WebRequest.Create(url); //创建request
                    if (proxy != null)
                    {
                        request.Proxy = proxy;//可能你在一些环境上不了网,得使用代理服务器
                    }
    var response = request.GetResponse(); //获取response var stream = response.GetResponseStream(); var responseReader = new StreamReader(stream); var content = responseReader.ReadToEnd(); //读取response(页面对应的html) var match = Regex.Match(content, Pattern, RegexOptions.IgnorePatternWhitespace); //筛选内容 return FilterMatch(match); } } public class YouDaoScaper : GetScraperBase { protected override string Pattern { 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>[ 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 @"<divsclass=""trans-container"">[ s]* <ul>[ s]* (<li>(?<content>.+)</li>[ s]*)* </ul>"; } } protected override Dictionary<string, string> FilterMatch(Match match) { var dict=new Dictionary<string, string>(); var content = ""; var group=match.Groups["content"]; if(group.Success) { foreach (Capture capture in group.Captures) { content += (capture.Value + " "); } } dict["content"]=content; return dict; } public string QueryWord(string word) { var url= "http://dict.youdao.com/search?q="+word; var dict = Scrape(url); return dict["content"]; } } }
     
  • 相关阅读:
    paramiko连接sshd使用的hostkey
    jmete命令行停止失败的原因分析
    send-mail: fatal: parameter inet_interfaces: no local interface found for ::1
    linux安装mail服务使用外部MTA发送邮件
    crontab使用简介
    pip命令自动补全功能;设置代理;使用国内源
    grep使用正则表达式搜索IP地址
    python执行系统命令的几种方法
    js 字符串 replace replaceAll
    tomcat 和 jboss的热部署(热发布)问题
  • 原文地址:https://www.cnblogs.com/caption/p/3966452.html
Copyright © 2020-2023  润新知