WebClient与WebRequest差异
WebRequst的使用
WebClient和HttpWebRequst是用来获取数据的2种方式,在我的这篇数据访问(2)中主要是讲的WebClient的使用,一般而言,WebClient更倾向于“按需下载”,事实上掌握它也是相对容易的,而HttpWebRequst则允许你设置请求头或者对内容需要更多的控制,后者有点类似于form中的submit。虽然两者都是异步请求事件,但是WebClient是基于事件的异步,而HttpWebRequst是基于代理的异步编程,下面就用简单的需求两者比较用法上的不同:
需求很简单,获取Web端的图片然后显示出来,结构如右边所示
UI很简单:
<StackPanel Background="White">
<Button Width="250"
Content="HttpWebRequest"
Click="Button_Click" />
<Button Width="250"
Content="Click for request with WebClient"
Click="Button_Click_1" />
<TextBox Text="1"
x:Name="numTextBox"
Width="20" />
<Image Height="150"
Name="image1"
Stretch="Fill"
Width="200" />
</StackPanel>
页面上提供一个TextBox用来输入文件名的,先看一看WebClient获取图片并显示在Image的过程
//使用WebClient
private void Button_Click_1(object sender, RoutedEventArgs e)
{
string baseUri = String.Format("http://localhost:49280/Images/{0}.jpg", this.numTextBox.Text.Trim());
Uri uri = new Uri(baseUri, UriKind.Absolute);
WebClient client = new WebClient();
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
}
void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
Stream stream = e.Result;
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(stream);
this.image1.Source = bitmap;
}
因为之前已经对WebClient总结过了,所以就不再重复了,主要是看一看WebRequst如果要实现相同的代码的过程
//使用WebRequest
private void Button_Click(object sender, RoutedEventArgs e)
{
string baseUri =String.Format("http://localhost:49280/Images/{0}.jpg",this.numTextBox.Text.Trim());
HttpWebRequest request =(HttpWebRequest) WebRequest.Create(baseUri);
request.Method = "GET";
request.BeginGetResponse(new AsyncCallback(ReadCallback), request);
}
public void ReadCallback(IAsyncResult asyc)
{
HttpWebRequest request = (HttpWebRequest)asyc.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyc);
this.Dispatcher.BeginInvoke(() =>
{
Stream stream = response.GetResponseStream();
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(stream);
this.image1.Source = bitmap;
}
);
}
几点需要注意的地方: 1,HttpWebRequest是个抽象类,所以无法new的,需要调用HttpWebRequest.Create();
2,其Method指定了请求类型,这里用的GET,还有POST;也可以指定ConentType;
3,其请求的Uri必须是绝对地址;
4,其请求是异步回调方式的,从BeginGetResponse开始,并通过AsyncCallback指定回调方法;
5,因为其回调不是UI线程,所以不能直接对UI进行操作,这里使用Dispatcher.BeginInvoke()
主要是第4点,如果把上面的代码中回调方法改成这样下面的样子的话,VS会提示跨域线程访问无效
HttpWebRequest request = (HttpWebRequest)asyc.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyc);
Stream stream=response.GetResponseStream();
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(stream);
this.image1.Source = bitmap;
转载于:https://www.cnblogs.com/626498301/archive/2010/08/13/1798662.html
WebClient与WebRequest差异相关推荐
- 第三节:总结.Net下后端的几种请求方式(WebClient、WebRequest、HttpClient)
一. 前言 前端调用有Form表单提交,ajax提交,ajax一般是用Jquery的简化写法,在这里不再过多介绍: 后端调用大约有这些:WebCient.WebRequest.Httpclient.W ...
- 利用WebClient和WebRequest类获得网页源代码C#
作者:不详 请速与本人联系 GetPageHtml.aspx <%@ Page language="c#" validateRequest = "fal ...
- c#利用WebClient和WebRequest获取网页源代码的比较
前几天举例分析了用asp+xmlhttp获取网页源代码的方法,但c#中一般是可以利用WebClient类和WebRequest类获取网页源代码.下面分别说明这两种方法的实现. WebClient类获取 ...
- webclient 和 webrequest获取网页源码的
private void WebClientButton_Click(object sender, System.EventArgs e) { PageUrl = UrlText.Text; WebC ...
- C# 读取网页源码的三种办法WebClient、WebRequest、HttpWebRequest
直接看这三种办法的源码吧, using System; using System.IO; using System.Net;namespace ReadHtml{ class ReadHtml{ st ...
- NET 进阶--WebClient和WebRequest
文章目录 WebClient WebRequest WebResponse WebClient WebClient类位于System.Net命名空间下,WebClient类提供向URI标识的任何本地. ...
- C#-WebClient
using ( var wc = new System.Net.WebClient() ) {var imagebytes = wc.DownloadData( hag.ImagePath );usi ...
- C#中HttpWebRequest、WebClient、HttpClient 、HttpClientFactory Flurl、的使用总结
C#中HttpWebRequest.WebClient.HttpClient .HttpClientFactory. Flurl的使用总结 三者的区别 HttpWebRequest 命名空间: Sys ...
- JAVA爬虫三大运营商
此代码仅供个人学习.研究之用,请勿用于商业用途 移动采集 import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSON ...
最新文章
- Asp.net支持的最大上传文件大小
- 2006鄂土整项目精神
- Some inputs do not have OOB scores. This probably means too few trees were used to compute any relia
- ProjectEuler500 【组合数学】【数论】
- HTTP basic auth
- 数据库技术基础:常见基本模型介绍笔记
- 电脑测速软件_网速慢,怎么办,教你测速,教你解决方案
- java 指定时间转换_Java中使用Calendar进行获取指定时间,使用SimpleDateFormat进行格式化转换...
- 安装配置mac版_全面战争三国 Mac版Mod安装指南
- 开源网管工具 汇总比较
- GAN详解与PyTorch MINIST手写数字生成实战
- 绝对受用的求职经验分享
- 三款截图软件:Snipaste+FastStone-Capture+FireShot
- 网络安全-靶机dvwa之sql注入Low到High详解(含代码分析)
- CDN 缓存与浏览器缓存
- HTML 的静态网页分页样式
- Django框架学习14--admin优化xadmin
- 正则表达式中 前瞻,后顾,负前瞻,负后顾等整理
- 一文读懂机智云物联网APP开发
- 第6章第9节:幻灯片背景:使用纹理和图像作为幻灯片的背景 [PowerPoint精美幻灯片实战教程]
热门文章
- 火狐 和 IE 透明度的设置。
- 又做了3个极品菜[图]
- Java13的API_JAVA基础--JAVA API常见对象(其他API)13
- iwrite提交不了作业_iWrite英语写作教学与评阅系统移动端——学生使用手册
- 计算机网络ipv4到ipv6怎么实现,论计算机网络协议IPV4到IPV6的过渡策略|房屋搬迁过渡协议...
- layui列表筛选列_Shopify搜索产品并筛选产品列表功能介绍
- php 递归展现城市信息,PHP 递归兑现层级树状展现数据
- 计算机ip地址未修复连接不了无线网络,本地连接没有有效的ip配置,教您修复本地连接没有有效的ip配置...
- vue 子父组件周期顺序_父组件和子组件生命周期钩子执行顺序是什么?
- jquery ajax是什么意思,Jquery ajax