首页 > 代码库 > 【C#】Get the html code of a webpage
【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 WebPageCode10 {11 12 class Program13 {14 15 [DllImport("kernel32.dll")]16 static extern uint GetTickCount(); 17 18 static void addhead(ref string px) //add http head if necessary19 {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 check28 WebClient wc = new WebClient(); 29 wc.Credentials = CredentialCache.DefaultCredentials; 30 31 proxy.Trim(); //proxy check32 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 =http://www.mamicode.com/ 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 found65 string code;66 if (proxy == ""){67 code=getwebcode(page); //proxy found68 } else {69 code=getwebcode(page,proxy); //no proxy found70 }71 72 Console.Write(code);73 Console.ReadKey();74 }75 }76 }
【C#】Get the html code of a webpage
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。