HttpWebRequest和WebClient的区别
1,HttpWebRequest是个抽象类,所以无法new的,需要调用HttpWebRequest.Create();
2,其Method指定了请求类型,这里用的GET,还有POST;也可以指定ConentType;
3,其请求的Uri必须是绝对地址;
4,其请求是异步回调方式的,从BeginGetResponse开始,并通过AsyncCallback指定回调方法;
5,WebClient方式使用基于事件的异步编程模型,在HTTP响应返回时引发的WebClient回调是在UI线程中调用的,因此可用于更新UI元素的性。HttpWebRequest是基于后台进程运行的,回调不是UI线程,所以不能直接对UI进行操作,通常使用Dispatcher.BeginInvoke()跟界面进行通讯。
HTTP中GET请求与POST请求的区别:
1.GET请求是从服务器上获取数据 POST请求是向服务器传送数据
2.GET请求在通过URL提交数据,数据在URL中可以看到 POST请求数据是通过写入数据流的方式提交
3.GET请求提交的数据最多只能1024个字节 POST请求提交的数据没有限制
HttpWebRequest类实现GET请求
1.通过HttpWebRequest.CreateHttp()方法可以创建一个HttpWebRequest对象;
2.设置请求的参数(如设置请求方式为GET);
3.发起异步响应请求:BeginGetGetResponse;
4.结束异步响应EndGetResponse;
5.获取请求的内容;
6.对请求进行异常处理。
通过下面的例子增加对HttpWebRequest类的GET请求方式的理解:
1 public void httpWebRequestGet() 2 { 3 try{ 4 String uri = "http://www.baidu.com/ "; 5 WebRequest request = HttpWebRequest.CreateHttp(new Uri(uri)); //创建WebRequest对象 6 request.Method = "GET"; //设置请求方式为GET 7 request.BeginGetResponse(responseStreamCallback, request); //发起GetResponse请求,返回应答请求异步操作的状态 8 } 9 catch(WebException e) 10 { 11 outputBlock.Text += " 出现异常请求失败! "; 12 outputBlock.Text += "异常信息: "; 13 outputBlock.Text += e.Message; 14 outputBlock.Text += " Status: "; 15 outputBlock.Text += e.Status; 16 } 17 catch(Exception e) 18 { 19 outputBlock.Text += " 出现异常请求失败! "; 20 outputBlock.Text += " 异常信息: "; 21 outputBlock.Text += e.Message; 22 } 23 } 24 25 private void responseCallback(IAsyncResult result) 26 { 27 try{ 28 HttpWebRequest request = (HttpWebRequest)result.AsyncState; //获取异步操作返回的的信息 29 WebResponse response = request.EndGetResponse(result); 30 using (Stream stream = response.GetResponseStream()) //获取请求返回内容 31 using(StreamReader read = new StreamReader(stream)) 32 { 33 string text = read.ReadToEnd(); 34 Dispatcher.BeginInvoke(() => //使用Dispatcher.BeginInvoke()更新UI信息 35 { 36 textBlock1.Text = text; 37 }); 38 } 39 40 } 41 //异常处理 42 catch(WebException e) 43 { 44 outputBlock.Text += " 出现异常请求失败! "; 45 outputBlock.Text += "异常信息: "; 46 outputBlock.Text += e.Message; 47 outputBlock.Text += " Status: "; 48 outputBlock.Text += e.Status; 49 } 50 catch(Exception e) 51 { 52 outputBlock.Text += " 出现异常请求失败! "; 53 outputBlock.Text += " 异常信息: "; 54 outputBlock.Text += e.Message; 55 } 56 }
HttpWebRequest类实现POST请求
1.通过HttpWebRequest.CreateHttp()方法可以创建一个HttpWebRequest对象;
2.设置请求的参数(如设置请求方式为POST)
3.发起获取要发送的数据流请求:BeginGetRequestStreamGetResponseStream请求;
4.结束异步的数据流请求:EndGetRequestStream;
5.把要传递的数据写入数据流;
6.发起异步响应请求:BeginGetGetResponse;
7.结束异步响应EndGetResponse ;
8.获取请求的内容;
9.对请求进行异常处理
通过下面的例子增加对HttpWebRequest类的POST请求方式的理解:
1 public void httpWebRequestPost() 2 { 3 try{ 4 String uri = "http://www.baidu.com/ "; 5 WebRequest request = HttpWebRequest.CreateHttp(new Uri(uri)); //创建WebRequest对象 6 request.Method = "POST"; //设置请求方式为POST 7 request.BeginGetRequestStream(responseStreamCallback, request); //发起GetResponseStream请求,返回应答请求异步操作的状态 8 } 9 catch(WebException e) 10 { 11 outputBlock.Text += " 出现异常请求失败! "; 12 outputBlock.Text += "异常信息: "; 13 outputBlock.Text += e.Message; 14 outputBlock.Text += " Status: "; 15 outputBlock.Text += e.Status; 16 } 17 catch(Exception e) 18 { 19 outputBlock.Text += " 出现异常请求失败! "; 20 outputBlock.Text += " 异常信息: "; 21 outputBlock.Text += e.Message; 22 } 23 } 24 //发起获取发送数据流的请求的响应 25 26 peivate void responseStreamCallback(IAsyncResult result) 27 { 28 HttpWebRequest request = (HttpWebRequest)result.AsyncState; //获取异步操作返回的的信息 29 Stream stream = httpWebRequest.EndGetRequestStream(result)) 30 31 String str = "hello world!"; 32 byte[] data = Encoding.UTF8.GetBytes(str); 33 stream.Write(data,0,data.Length); 34 stream.Close(); 35 36 request.BeginGetResponse(responseCallback, request); 37 } 38 39 private void responseCallback(IAsyncResult result) 40 { 41 try{ 42 HttpWebRequest request = (HttpWebRequest)result.AsyncState; //获取异步操作返回的的信息 43 WebResponse response = (WebResponse)request.EndGetResponse(result); 44 using (Stream stream = response.GetResponseStream()) 45 using ( StreamReader read = new StreamReader(stream) 46 { 47 string text = read.ReadToEnd(); 48 Dispatcher.BeginInvoke(() => //使用Dispatcher.BeginInvoke()更新UI信息 49 { 50 textBlock.Text = text; 51 }); 52 } 53 54 } 55 //异常处理 56 catch(WebException e) 57 { 58 outputBlock.Text += " 出现异常请求失败! "; 59 outputBlock.Text += "异常信息: "; 60 outputBlock.Text += e.Message; 61 outputBlock.Text += " Status: "; 62 outputBlock.Text += e.Status; 63 } 64 catch(Exception e) 65 { 66 outputBlock.Text += " 出现异常请求失败! "; 67 outputBlock.Text += " 异常信息: "; 68 outputBlock.Text += e.Message; 69 } 70 }
文章为作者原创,转载请标明出处,谢谢 http://www.cnblogs.com/spilledlight/p/4929418.html