如何以编程方式获取URL的页面源代码? [英] How to get a page source code of URL programmatically?
问题描述
从C#例程我需要转到URL页面并绕过浏览器下载该页面的HTML源代码。然后我打算分析代码并提取另一个URL - 但我已经知道如何处理它。我希望这个页面在内存中,作为一个字符串变量,而不是一个磁盘文件 - 它是一个非常临时的用途。
From a C# routine I need to go to a URL page and download the HTML source code of that page bypassing the browser. I then intent to analyze the code and extract another URL -- but I already know how to handle it. I prever to have this page in memory, as a string variable, not as a disk file--it is intended for a very temporary usage.
我面临的挑战是弄清楚如何让程序下载HTML代码而不提醒Internet Explorer浏览器。
The challenge for me is to figure out how to have the program download the HTML code without alerting the Internet Explorer Browser.
我确信这对于longtimer来说是一个简单的问题。希望得到帮助。
I am sure it is a simple problem for a longtimer. Hope for help.
谢谢
推荐答案
您可以使用HttpWebRequest类获取html页面:
You can fetch the html page using HttpWebRequest class:
HttpWebRequest request =( HttpWebRequest ) HttpWebRequest .Create(url) ;使用 ( / font> stream = request.GetResponse()。GetResponseStream())
{
using ( StreamReader reader = new StreamReader (stream))
{
string response = reader.ReadToEnd();
}
}
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
using (Stream stream = request.GetResponse().GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream))
{
string response = reader.ReadToEnd();
}
}
Andrej
这篇关于如何以编程方式获取URL的页面源代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!