Fetching a string with httpwebrequest and

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

Example 13-13. Fetching a string with HttpWebRequest and HttpWebResponse HttpWebRequest req = (HttpWebRequest) WebRequest.Create(" "); using (HttpWebResponse resp = (HttpWebResponse) req.GetResponse()) using (Stream respStream = resp.GetResponseStream()) using (StreamReader reader = new StreamReader(respStream)) { string pageContent = reader.ReadToEnd(); Console.WriteLine(pageContent); } The two casts on the first two lines of Example 13-13 are a little messy, but are, un- fortunately, usually necessary. The WebRequest family of classes is extensible to multiple protocols, so most of the methods are declared as returning the abstract base types, rather than the concrete types—the exact type returned depends on the kind of URL you use. So if you need access to a protocol-specific feature, you end up with a cast. In fact, Example 13-13 isn’t using anything protocol-specific, so we could have avoided the casts by declaring req and resp as WebRequest and WebResponse , respectively. How- ever, the usual reason for using these classes is that you do in fact want access to HTTP- specific information. For example, you might want to simulate a particular web browser by setting the user agent string, as shown in Example 13-14 . Example 13-14. Changing the user agent header with HttpWebRequest HttpWebRequest req = (HttpWebRequest) WebRequest.Create(" "); req.UserAgent = "Mozilla/5.0 (iPod; U; CPU iPhone OS 2_2_1 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Mobile/5H11a"; ... as before 516 | Chapter 13: Networking
Image of page 540

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

This code has been split across multiple lines, as the user agent string is too wide to fit. This would let you discover what response a website would send if the request came from an Apple iPhone. (Many websites adapt their content for different devices.) As you’d expect, asynchronous operation is available so that you can avoid blocking the current thread while waiting for network operations to complete. But it looks slightly different from the WebClient mechanisms we’ve seen so far, because of the way in which the methods you call can change when the request gets sent. No network communication happens at the point where you create the request, so there is no asyn- chronous method for that. Remember, the request object represents all the settings you’d like to use for your HTTP request, so it won’t actually attempt to send anything until you’ve finished setting the request’s properties and tell it you’re ready to proceed. There are two ways in which you can cause an HttpWebRequest to send the request. Asking for the response object will cause this, but so will asking for a request stream— the request’s GetStream method returns a write-only stream that can be used to supply the body of the request for POST or similar verbs (much like WebClient.OpenWrite ). This stream will start sending data over the network as soon as your code writes data into the stream—it doesn’t wait until you close the stream to send the data all in one go.
Image of page 541
Image of page 542
This is the end of the preview. Sign up to access the rest of the document.
  • Spring '15

{[ 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