从网页下载源代码时遇到问题

发布于 2024-12-04 22:30:17 字数 614 浏览 1 评论 0原文

我正在尝试使用 c# 下载此网页的源代码,用于学校项目。
这是我想要获取的页面:
http://www.epicurious.com/tools/fooddictionary/entry?id=1650

我已经尝试过诸如

 HttpWebRequest request = (HttpWebRequest)
            WebRequest.Create("http://www.epicurious.com/tools/fooddictionary/entry?id=1650");

和 by using 之

 WebClient client = new WebClient();
string value = client.DownloadString("http://www.epicurious.com/tools/fooddictionary/entry?id=1650");

类的代码,但两种方法都无法获取页面源代码。任何帮助将不胜感激。

Im trying to download the source code for this webpage for a school project using c#.
this is the page im trying to get:
http://www.epicurious.com/tools/fooddictionary/entry?id=1650

I have tried code such as

 HttpWebRequest request = (HttpWebRequest)
            WebRequest.Create("http://www.epicurious.com/tools/fooddictionary/entry?id=1650");

and by using

 WebClient client = new WebClient();
string value = client.DownloadString("http://www.epicurious.com/tools/fooddictionary/entry?id=1650");

and neither methods are getting me the page source. Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

香草可樂 2024-12-11 22:30:17

使用 HttpWebRequest.Create

try
{
    WebRequest req = HttpWebRequest.Create("http://www.epicurious.com/tools/fooddictionary/entry?id=1650");
    req.Method = "GET";

    string source;
    using (StreamReader reader = new StreamReader(req.GetResponse().GetResponseStream()))
    {
        source = reader.ReadToEnd();
    }
}
catch (Exception ex)
{
    //Log the exception 
    MessageBox.Show(ex.ToString());
}

使用 DownloadString

WebClient client = new WebClient();
string reply = client.DownloadString("http://www.epicurious.com/tools/fooddictionary/entry?id=1650");

以上方法对我来说效果很好。检查是否有异常..

using HttpWebRequest.Create

try
{
    WebRequest req = HttpWebRequest.Create("http://www.epicurious.com/tools/fooddictionary/entry?id=1650");
    req.Method = "GET";

    string source;
    using (StreamReader reader = new StreamReader(req.GetResponse().GetResponseStream()))
    {
        source = reader.ReadToEnd();
    }
}
catch (Exception ex)
{
    //Log the exception 
    MessageBox.Show(ex.ToString());
}

Using DownloadString

WebClient client = new WebClient();
string reply = client.DownloadString("http://www.epicurious.com/tools/fooddictionary/entry?id=1650");

Above methods are working fine for me. Check the exception if any..

最笨的告白 2024-12-11 22:30:17

这个怎么样...

string sourceCode = "";
Uri site = new Uri("http://www.epicurious.com/tools/fooddictionary/entry?id=1650");
WebRequest request = WebRequest.Create(site);
using(StreamReader reader = new StreamReader(request.GetResponse().GetResponseStream(), Encoding.ASCII)){
    sourceCode = reader.ReadToEnd();
}

“using”语句将为您关闭您的流。注意:在流上调用 close 也会在它正在使用的任何流上调用类,这就是为什么您只需要单个 using 语句

How about this...

string sourceCode = "";
Uri site = new Uri("http://www.epicurious.com/tools/fooddictionary/entry?id=1650");
WebRequest request = WebRequest.Create(site);
using(StreamReader reader = new StreamReader(request.GetResponse().GetResponseStream(), Encoding.ASCII)){
    sourceCode = reader.ReadToEnd();
}

The "using" statement will close your streams for you. NOTE: Calling close on a stream will also call class on any streams it is using, this is why you only need the single using statement

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文