首页 > 代码库 > 网络编程之WebClient,WebRequest和WebResponse类

网络编程之WebClient,WebRequest和WebResponse类

一.基本的WebClient类

1.首先使用OpenRead()方法返回一个stream流对象

2.把Stream流对象转换为StreamReader对象

3.使用ReadLine()方法从数据流中以文本的方式获取数据

            WebClient web = new WebClient();            Stream stream = web.OpenRead("http://www.reuters.com");            StreamReader sr = new StreamReader(stream);            while (sr.ReadLine() != null)            {                string line;                line = sr.ReadLine();                listBox1.Items.Add(line);            }            stream.Close();

二.WebRequest和WebResponse类

.WebRequest代表要给某个特定URI发送信息的请求,uri作为参数传递给Create()方法,WebResponse类代表从服务器检索的数据

1.创建新的线程来请求页面

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.Net;using System.IO;using System.Threading;namespace WindowsFormsApplication1{    public delegate void mydelegate(WebHeaderCollection s); //声明委托    public partial class Form1 : Form    {        Thread x;        public Form1()        {            InitializeComponent();            WebRequest wrq = WebRequest.Create("http://www.reuters.com");            //定义一个线程            x = new Thread(doresponse);            //启动线程            x.Start(wrq);        }        private void doresponse(object ar)        {            WebRequest wrq = (WebRequest)ar;            WebResponse wrp = wrq.GetResponse();            WebHeaderCollection whc = wrp.Headers;//wrp.Headers:获取与请求相关联的标头值的集合            mydelegate my = new mydelegate(settext);//声明委托执行的方法为settext()            this.Invoke(my, new object[] { whc });//在拥用此控件的基础窗体句柄的线程上执行指定的委托,此方法第二参数用于传入方法,代替形参whc        }                private void settext(WebHeaderCollection whc)        {            for (int i = 0; i < whc.Count; i++)            {                listBox1.Items.Add(string.Format("name {0}:{1}", whc.GetKey(i), whc[i]));//GetKey(i)代表获取指定索引的标头名称,whc[i]和whc.Get(i)代表获取特定标头名称的值                listBox1.Items.Add(string.Format("name name {0}:{1}", whc.GetKey(i), whc.Get(i)));            }            x.Abort();//关闭这个线程        }    }}

2.使用异步方法进行页面请求

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.Net;using System.IO;using System.Threading;namespace WindowsFormsApplication1{    public delegate void mydelegate(WebHeaderCollection s);     public partial class Form1 : Form    {        Thread x;        public Form1()        {            InitializeComponent();            WebRequest wrq = WebRequest.Create("http://www.reuters.com");            //把身份验证证书附在请求中            NetworkCredential myCredentials = new NetworkCredential("lzw", "123");            HttpWebRequest hwrq = (HttpWebRequest)wrq;//可以在任何需要WebRequest类的地方使用派生类HttpWebRequest            hwrq.Credentials = myCredentials;//获取身份验证信息
//HttpWebRequest类特有的属性: listBox1.Items.Add("等待异常抛出的时间:" + hwrq.Timeout);//hwrq.Timeout默认值100000毫秒 listBox1.Items.Add("是否与 Internet 资源建立持久性连接:" + hwrq.KeepAlive);//默认为true listBox1.Items.Add("是否自动跟随web重定向响应:" + hwrq.AllowAutoRedirect);//默认为true //异步页面请求 IAsyncResult ar = hwrq.BeginGetResponse(null, null); hwrq = (HttpWebRequest)ar.AsyncState; WebResponse wrp = wrq.EndGetResponse(ar); WebHeaderCollection whc = wrp.Headers;//wrp.Headers:获取与请求相关联的标头值的集合 for (int i = 0; i < whc.Count; i++) { listBox1.Items.Add(string.Format("name {0}:{1}", whc.GetKey(i), whc[i]));//GetKey(i)代表获取指定索引的标头名称,whc[i]和whc.Get(i)代表获取特定标头名称的值 listBox1.Items.Add(string.Format("name name {0}:{1}", whc.GetKey(i), whc.Get(i))); } } }}