WebClient与WebRequest差异
WebRequst的使用
???? WebClient和HttpWebRequst是用來(lái)獲取數(shù)據(jù)的2種方式,在我的這篇數(shù)據(jù)訪問(wèn)(2)中主要是講的WebClient的使用,一般而言,WebClient更傾向于“按需下載”,事實(shí)上掌握它也是相對(duì)容易的,而HttpWebRequst則允許你設(shè)置請(qǐng)求頭或者對(duì)內(nèi)容需要更多的控制,后者有點(diǎn)類(lèi)似于form中的submit。雖然兩者都是異步請(qǐng)求事件,但是WebClient是基于事件的異步,而HttpWebRequst是基于代理的異步編程,下面就用簡(jiǎn)單的需求兩者比較用法上的不同:
??? 需求很簡(jiǎn)單,獲取Web端的圖片然后顯示出來(lái),結(jié)構(gòu)如右邊所示
?
UI很簡(jiǎn)單: <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>
? 頁(yè)面上提供一個(gè)TextBox用來(lái)輸入文件名的,先看一看WebClient獲取圖片并顯示在Image的過(guò)程
//使用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; }因?yàn)橹耙呀?jīng)對(duì)WebClient總結(jié)過(guò)了,所以就不再重復(fù)了,主要是看一看WebRequst如果要實(shí)現(xiàn)相同的代碼的過(guò)程
//使用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; } ); }幾點(diǎn)需要注意的地方: 1,HttpWebRequest是個(gè)抽象類(lèi),所以無(wú)法new的,需要調(diào)用HttpWebRequest.Create();
??????????????????????????? 2,其Method指定了請(qǐng)求類(lèi)型,這里用的GET,還有POST;也可以指定ConentType;
??????????????????????????? 3,其請(qǐng)求的Uri必須是絕對(duì)地址;
??????????????????????????? 4,其請(qǐng)求是異步回調(diào)方式的,從BeginGetResponse開(kāi)始,并通過(guò)AsyncCallback指定回調(diào)方法;
??????????????????????????? 5,因?yàn)槠浠卣{(diào)不是UI線程,所以不能直接對(duì)UI進(jìn)行操作,這里使用Dispatcher.BeginInvoke()
主要是第4點(diǎn),如果把上面的代碼中回調(diào)方法改成這樣下面的樣子的話,VS會(huì)提示跨域線程訪問(wèn)無(wú)效
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;轉(zhuǎn)載于:https://www.cnblogs.com/626498301/archive/2010/08/13/1798662.html
總結(jié)
以上是生活随笔為你收集整理的WebClient与WebRequest差异的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 扣件多少钱啊?
- 下一篇: Acer Travelmate T329