zoukankan      html  css  js  c++  java
  • 【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 }
  • 相关阅读:
    论文阅记 YOLOv4: Optimal Speed and Accuracy of Object Detection
    【项目实战】yolov3-tiny人脸数据模型训练
    面试题54. 二叉搜索树的第k大节点
    102. 二叉树的层序遍历
    107. 二叉树的层次遍历 II
    连续子数组的最大和
    172. 阶乘后的零
    26 进制
    405. 数字转换为十六进制数
    504. 七进制数
  • 原文地址:https://www.cnblogs.com/Fefnir/p/5874368.html
Copyright © 2011-2022 走看看