As well as providing completion event notifications

Info icon This preview shows pages 538–540. Sign up to view the full content.

View Full Document Right Arrow Icon
As well as providing completion event notifications, WebClient also offers progress no- tifications through its DownloadProgressChanged event. This is raised from time to time during asynchronous downloads, regardless of which of the three methods you used. It provides two properties, BytesReceived and TotalBytesToReceive , which tell you how far the download has gotten and how far it has to go. If you use these asynchronous methods in a GUI built with either WPF or Windows Forms, you don’t need to worry about threading issues. As you’ll see in later chapters, that is not true for all asynchronous APIs, but these automatically take care of UI threading for you—as long as you start asynchronous operations from the UI thread, WebClient will raise completion and progress events on the UI thread. Uploading resources WebClient offers the UploadString , UploadData , and UploadFile methods. These corre- spond directly to the DownloadString , DownloadData , and DownloadFile methods, but instead of fetching data with an HTTP GET , they send data to the server, typically using an HTTP POST , although overloads are available that let you specify other verbs, such as PUT . Stream-based uploads and downloads Lots of APIs in the .NET Framework work with the Stream abstraction defined in the System.IO namespace. The XML classes can load data from a Stream , or write data into one, for example. The bitmap decoding and encoding classes in WPF can also work with streams. The first three lines of Example 13-12 obtain a stream for an Atom feed # from a WebClient and use it to initialize an XDocument . The code then uses LINQ to XML to extract the list of titles and links advertised by this particular feed. Example 13-12. From HTTP to LINQ to XML via a Stream WebClient client = new WebClient(); Stream feedStm = client.OpenRead("http://feeds.feedburner.com/oreilly/news"); XDocument feedXml = XDocument.Load(feedStm); string ns = "http://www.w3.org/2005/Atom"; var entries = from entryElement in feedXml.Descendants(XName.Get("entry", ns)) #Atom is a common format for representing sets of items, such as blog entries or news articles. It’s similar to RSS, but tries to avoid some of RSS’s inconsistencies and limitations. 514 | Chapter 13: Networking
Image of page 538

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
select new { Title = entryElement.Element(XName.Get("title", ns)).Value, Link = entryElement.Element(XName.Get("link", ns)). Attribute("href").Value }; foreach (var entry in entries) { Console.WriteLine("{0}: {1}", entry.Title, entry.Link); } For sending data there’s an OpenWrite method. With HTTP or HTTPS, this defaults to POST , but as with the Upload methods, you can call an overload that takes the verb as well as the URL. You can use streams asynchronously. Following the same pattern as the other methods we’ve looked at so far, you’ll find OpenReadAsync and OpenWriteAsync methods, with corresponding completion events. But streams add an extra dimension: the Stream ab- stract base class also offers both synchronous and asynchronous operation. For exam- ple, if you’re reading data, you can call either Read or BeginRead . You are free to use the Stream
Image of page 539
Image of page 540
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern