当前位置:网站首页>C # three ways to obtain web page content
C # three ways to obtain web page content
2022-06-30 05:02:00 【StudyHard_ luozhongxu】
C# Three ways to get web content
Search the web , Find out C# There are usually three ways to get web content , Use WebClient、WebBrowser perhaps HttpWebRequest/HttpWebResponse...
Method 1 : Use WebClient ( Quote from :http://fbljava.blog.163.com/blog/static/265211742008712105145244/)
static void Main(string[] args)
{
try {
WebClient MyWebClient = new WebClient();
MyWebClient.Credentials = CredentialCache.DefaultCredentials;// Gets or sets the value used to Internet Network credentials for authentication of resource requestsByte[] pageData = MyWebClient.DownloadData(“http://www.163.com”); // Download data from the specified website
string pageHtml = Encoding.Default.GetString(pageData); // If you get the website page using GB2312, Use this sentence
//string pageHtml = Encoding.UTF8.GetString(pageData); // If you get the website page using UTF-8, Use this sentence
Console.WriteLine(pageHtml);// Enter the acquired content in the console
using (StreamWriter sw = new StreamWriter("c:\\test\\ouput.html"))// Write the acquired content into the text
{
sw.Write(pageHtml);
}
Console.ReadLine(); // Let the console pause , Otherwise, it will flash by
}
catch(WebException webEx) {
Console.WriteLine(webEx.Message.ToString());
}
}
Method 2 : Use WebBrowser ( Quote from :http://topic.csdn.net/u/20091225/14/4ea221cd-4c1e-4931-a6db-1fd4ee7398ef.html)
WebBrowser web = new WebBrowser();
web.Navigate("http://www.xjflcp.com/ssc/");
web.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(web_DocumentCompleted);
void web_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser web = (WebBrowser)sender;
HtmlElementCollection ElementCollection = web.Document.GetElementsByTagName("Table");
foreach (HtmlElement item in ElementCollection)
{
File.AppendAllText("Kaijiang_xj.txt", item.InnerText);
}
}
Method 3 : Use HttpWebRequest/HttpWebResponse ( Quote from :http://hi.baidu.com/onlyafar/blog/item/7ac4c6bf92d4810019d81f98.html)
HttpWebRequest httpReq;
HttpWebResponse httpResp;
string strBuff = "";
char[] cbuffer = new char[256];
int byteRead = 0;
string filename = @"c:\log.txt";
/// Define write stream operations
public void WriteStream()
{
Uri httpURL = new Uri(txtURL.Text);///HttpWebRequest Class inherited from WebRequest, Does not have its own constructor , Need to pass WebRequest Of Creat Method establish , And perform forced type conversion
httpReq = (HttpWebRequest)WebRequest.Create(httpURL);
/// adopt HttpWebRequest Of GetResponse() Method establishment HttpWebResponse, CasthttpResp = (HttpWebResponse) httpReq.GetResponse();
///GetResponseStream() Method to get HTTP The data flow of the response , And try to get URL Page content specified in/// If you succeed in getting the content of the web page , with System.IO.Stream returns , If it fails, there will be ProtoclViolationException wrong By mistake . The right way to do this is to put the following code in one try Processing in blocks . It's easy to deal with
Stream respStream = httpResp.GetResponseStream();/// The content returned is Stream Formal , So we can use StreamReader Class gets GetResponseStream The content of , And
StreamReader Class Read Method to read the content of each line of the web page source code in turn , To the end of the line ( Read the encoding format :UTF8)
StreamReader respStreamReader = new StreamReader(respStream,Encoding.UTF8);byteRead = respStreamReader.Read(cbuffer,0,256);
while (byteRead != 0)
{
string strResp = new string(cbuffer,0,byteRead);
strBuff = strBuff + strResp;
byteRead = respStreamReader.Read(cbuffer,0,256);
}
respStream.Close();
txtHTML.Text = strBuff;
}Pro test example :
/// <summary>
/// Access to web pages Html data
/// </summary>
protected void GetWebData()
{
Debug.Log("Getting web data...");
try
{
byte[] pageData = myWebClient.DownloadData(url);
string html = Encoding.UTF8.GetString(pageData);
Debug.Log("Gat web data.");
if (!string.IsNullOrEmpty(html))
{
Debug.Log("Web data is not null!");
Debug.Log("html" + html);
string startTag = "<body>";
string endTag = "</body>";
int start = html.IndexOf(startTag);
int end = html.IndexOf(endTag);
if (start > 0 && end > 0 && end > start)
{
Debug.Log("Data is not null!");
int len1 = start + startTag.Length;
string body = html.Substring(len1, end - len1);
Debug.Log("body:" + body);
}
else
Debug.Log("Data is null!");
}
else
Debug.Log("Web data is null!");
}
catch
{
Debug.Log("Not get web data!");
}
}This article is only for learning and reference ! Please correct me if there is any mistake !
边栏推荐
- OpenGL draws model on QT platform to solve the problem of initializing VAO and VBO
- 力扣27. 移除元素
- brew安装nvm报nvm command not found解决方案
- Unity profiler performance analysis
- Nestjs入门和环境搭建
- 0 basic unity course. Bricklaying
- Four methods of unity ugui button binding events
- The golden deer, a scenic spot in London -- a sailing museum that tells vivid sailing stories
- [recruitment] UE4 Development Engineer
- MySQL查询小工具(一)json格式的字符串字段中,替换json数组中对象的某个属性值
猜你喜欢

Marvel fan welfare: Singapore Madame Tussauds Wax Museum Marvel 4D experience Museum

Sailing experience not to be missed in New York Tourism: take you to enjoy the magnificent city scenery from different perspectives

Preorder traversal of Li Kou 589:n fork tree

Basic operations of Oracle data

Unity realizes rotation and Revolution

Arsenal Stadium Tour - take you to the front and back of Arsenal Stadium

力扣349. 两个数组的交集

【VCS+Verdi联合仿真】~ 以计数器为例

redis集群概念

Unreal 4 learning notes - data storage using blueprints
随机推荐
OpenGL draws model on QT platform to solve the problem of initializing VAO and VBO
Force buckle 349 Intersection of two arrays
【VCS+Verdi联合仿真】~ 以计数器为例
Meet in Bangkok for a romantic trip on Valentine's Day
Efficiency test of adding and querying ArrayList and LinkedList
Harbor API 2.0 query
Have a heart beating Valentine's day in Singapore
Force buckle 59 Spiral matrix II
Oculus quest2 development: (I) basic environment construction and guide package
Unity Logitech steering wheel access
Moore Manor diary I: realize the reclamation, sowing, watering and harvest in Moore Manor
Unity3d realizes Google Digital Earth
amd锐龙CPU A320系列主板如何安装win7
Redis cluster concept
Pytorchcnn image recognition and classification model training framework
MySQL query gadget (I) replace a property value of the object in the JSON array in the JSON format string field
Unreal 4 learning notes - Animated Montage
UnityEngine. JsonUtility. The pit of fromjason()
Unity enables simple music visualization
Connect() and disconnect() of socket in C #