• 【C#】Get the html code of a webpage


    As for the title,the console program will show you a sample which can get the webpage-code.Enjoy it:)

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 using System.Net;
     7 using System.IO;
     8 using System.Runtime.InteropServices; //dllimport relies on it
     9 namespace WebPageCode
    10 {
    11     
    12     class Program
    13     {
    14         
    15         [DllImport("kernel32.dll")]
    16         static extern uint GetTickCount();  
    17 
    18         static void addhead(ref string px) //add http head if necessary
    19         {
    20             if (px.Substring(0, 7).ToLower() != @"http://") px = @"http://" + px;
    21         }
    22 
    23         static string getwebcode(string PageUrl,string proxy=""){
    24 
    25             uint x = GetTickCount();
    26 
    27             addhead(ref PageUrl);  //webpage check
    28             WebClient wc = new WebClient(); 
    29             wc.Credentials = CredentialCache.DefaultCredentials; 
    30             
    31             proxy.Trim();  //proxy check
    32             if (proxy != "")
    33             {
    34                 addhead(ref proxy);
    35                 WebProxy wp = new WebProxy();
    36                 wp.Address = new Uri(proxy);
    37                 wc.Proxy = wp;
    38             }
    39 
    40             Byte[] pageData = wc.DownloadData(PageUrl); 
    41             x = GetTickCount() - x; //x means the time spent.
    42             return Encoding.UTF8.GetString(pageData);
    43 
    44         }
    45 
    46         static void Main(string[] args)
    47         {
    48             int i = 0, n = args.Length;
    49             string proxy = "";
    50             string page = "";
    51             while (i < n)
    52             {
    53                 if (args[i] == "-proxy")
    54                 {
    55                     if (i<(n-1)) proxy = args[++i];
    56                 }
    57                 if (args[i] == "-page")
    58                 {
    59                     if (i < (n - 1)) page = args[++i];
    60                 }
    61                 i++;
    62             }
    63             page.Trim(); proxy.Trim();
    64             if (page == "") return; //no webpage found
    65             string code;
    66             if (proxy == ""){
    67                 code=getwebcode(page);  //proxy found
    68             } else {
    69                 code=getwebcode(page,proxy);  //no proxy found
    70             }
    71             
    72             Console.Write(code);
    73             Console.ReadKey();
    74         }
    75     }
    76 }
  • 相关阅读:
    向存储过程中传入列表参数
    TRUNCATE TABLE (Transact-SQL)
    Program.cs
    联想G40-30 安装win10系统
    完美解决github访问速度慢[转]
    关于Visual studio 2017安装方法的若干问题
    [转载] 中国象棋软件-引擎实现(七)测试程序
    [转载] 中国象棋软件-引擎实现(六)局面评估
    [转载] 中国象棋软件-引擎实现(五)历史启发及着法排序
    [转载] 中国象棋软件-引擎实现(四)搜索算法
  • 原文地址:https://www.cnblogs.com/Fefnir/p/5874368.html
Copyright © 2020-2023  润新知