c#网络编程-第一章

1.需求

获得网页数据,并填充到webbrowser空间中

2.代码示例

      

private void button1_Click_1(object sender, EventArgs e)
{
//1.浏览的url
string url = textBox1.Text;

//2.创建请求链接
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);


//3.获得响应实例
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); //GetResponse:获取答复

//4.构造数据流实例
Stream stream = response.GetResponseStream();
StreamReader sr = new StreamReader(stream);

//5.从流当前位置读取到末尾并显示在WebBrower控件中
string content = sr.ReadToEnd();
webBrowser1.DocumentText = content;

}

 

posted @ 2017-02-15 11:11  layfork  阅读(103)  评论(0编辑  收藏  举报