如何阅读网站 [英] how to read a website

查看:102
本文介绍了如何阅读网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一个非常简单的事情 - 点击一个网站并检索它的

给我(在C#中)。我不得不承认我在MSDN

文档中丢失了一点。我能想到的最好的是我应该使用

HttpWebRequest.Create(URL)建立连接,但我不知道
知道从那里去哪里。是否有任何在线文档显示示例代码

来检索返回的html?或者,对于

的调用顺序是什么,最后得到页面的字符串表示?


提前感谢

解决方案


http://groups.google.com/groups?hl=e...phx.gbl& rnum = 5

或者只是搜索HttpWebRequest Stream


干杯,


- -

Ignacio Machin,

ignacio.machin at dot.state.fl.us

Florida Department of Transportation

轴 <菊******************** @ nospam.com>在消息中写道

新闻:Aez4d.241303


Fg5.147680@attbi_s53 ...

我想做一个非常简单的事情事 - 打网站并检索它给我的html(在C#中)。我不得不承认我在MSDN
文档中有点遗失。我能想到的最好的是我应该使用
HttpWebRequest.Create(URL)建立连接,但我不知道从那里去哪里。是否有任何在线文档显示示例
代码来检索返回的html?或者,调用的顺序是什么?
最终获得页面的字符串表示?

提前致谢



Hi Axis,


这需要几步。以下代码是基本的方法,不使用任何标题


HttpWebRequest req =(HttpWebRequest)HttpWebRequest.Create(" http://www.google.com") ;


HttpWebResponse resp =(HttpWebResponse)req.GetResponse();


StreamReader sr = new StreamReader(resp.GetResponseStream());


//获取文本(StreamReader默认使用UTF-8,但你可以更改它)

string html = sr.ReadToEnd(); < br $>

//关闭溪流

sr.Close();

resp.Close();


-

快乐编码!

Morten Wennevik [C#MVP]


I want to do a very simple thing -- hit a website and retrieve the html it
gives me (in C#). I have to admit I''m a little lost within the MSDN
documentation. The best I can figure is I should use
HttpWebRequest.Create(URL) to being setting up the connection, but I don''t
know where to go from there. Are there any online docs that show sample code
to retrieve the html that''s returned? Or, what''s the sequence of calls to
finally get a string representation of the page?

Thanks in advance

解决方案

Hi,
http://groups.google.com/groups?hl=e...phx.gbl&rnum=5
Or just make a search for HttpWebRequest Stream

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"axis" <ju********************@nospam.com> wrote in message
news:Aez4d.241303


Fg5.147680@attbi_s53...

I want to do a very simple thing -- hit a website and retrieve the html it
gives me (in C#). I have to admit I''m a little lost within the MSDN
documentation. The best I can figure is I should use
HttpWebRequest.Create(URL) to being setting up the connection, but I don''t
know where to go from there. Are there any online docs that show sample code to retrieve the html that''s returned? Or, what''s the sequence of calls to
finally get a string representation of the page?

Thanks in advance



Hi Axis,

It requires a few steps. The following code is the basic way to do it, not using any headers

HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://www.google.com");

HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

StreamReader sr = new StreamReader(resp.GetResponseStream());

// get the text (StreamReader uses UTF-8 by default, but you can change it)
string html = sr.ReadToEnd();

//close the streams
sr.Close();
resp.Close();

--
Happy Coding!
Morten Wennevik [C# MVP]


这篇关于如何阅读网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆