日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

WebClient与WebRequest差异

發布時間:2023/12/1 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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差异的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。