SoFunction
Updated on 2025-03-01

How to get web page source code in C#

This article describes the method of obtaining web page source code in C#. Share it for your reference. The details are as follows:

public string GetPageHTML(string url)
{
 try
 {
  HttpWebRequest wr = (url) as HttpWebRequest;
   = "get";
   = "*/*";
  ("Accept-Language: zh-cn");
  ("UA-CPU: x86");
  ("Accept-Encoding: gzip, deflate");
   = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Embedded Web Browser from: /; InfoPath.2; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
   = true;
  .Expect100Continue = false;
   = false;
  HttpWebResponse wre = () as HttpWebResponse;
  StreamReader sreader = new StreamReader((), ("GBK"));
  string sHtml = ();
  ();
  return sHtml;
 }
 catch
 {
  return "";
 }
}

I hope this article will be helpful to everyone's C# programming.