- Hands-On Network Programming with C# and .NET Core
- Sean Burns
- 500字
- 2021-06-24 16:05:29
The class methods
Now that we have a sufficiently complete picture of the shape of the WebRequest class, let's explore its scope, or proper use. Let's take a look at its public methods. Understanding what's available through the base class will give you everything you need to leverage any concrete implementation in the vast majority of your use cases, with perhaps only minor modifications. So, just as we did with the class properties, let's take look at the following list of public methods and see what we can infer about how the class is meant to be used:
- Abort()
- BeginGetRequestStream(AsyncCallback, Object)
- BeginGetResponse(AsyncCallback, Object)
- Create(string)
- Create(Uri)
- CreateDefault(Uri)
- CreateHttp(string)
- EndGetRequestStream(IAsyncResult)
- EndGetResponse(IAsyncResult)
- GetObjectData(SerializationInfo, StreamingContext)
- GetRequestStream()
- GetRequestStreamAsync()
- GetResponse()
- GetResponseAsync()
- GetSystemWebProxy()
- RegisterPrefix(string, IWebRequestCreate)
I only included the methods specific to the WebRequest class, and left out the public methods inherited from parent classes, such as MarshalByRefObject and Object, since those aren't relevant to our purpose. However, with this basic list of operations, the utility of the class should be pretty obvious.
The first thing that likely stands out is that the class should be used asynchronously. All of the Begin and End methods, as well as the Async suffix on a number of other methods, tell you that the class supports fine-grained control of the lifetime of your requests through the asynchronous features of .NET Core. Now, if you've never done async programming (as I often find to be the case with newer programmers just starting out of school, or programmers new to web development) we'll be covering that mental leap in much greater detail in the next chapter. It's not always intuitively obvious how best to leverage the features of async, or what's going on behind the scenes; so, for now, just think of it as deferring the actual execution of the method until later. Just like all those methods suggest, you Begin doing a task, and whenever you're ready to, you End it and look at your result.
The methods in this class can be broken up into two conceptual groups. There are methods for state management and methods for request execution. The state management methods allow you to modify or further define the state of your instance of the WebRequest utility class. Leveraging them to further configure and define the behavior of your instance is similar to setting any of the public properties on the class as we did in the last section on Class properties. The reason there are methods to do this, instead of simply having more settable properties, is because doing so involves at least some non-trivial logic or circumstance-specific details that are applied with each invocation of the methods. Meanwhile, the request execution functions allow you to define, invoke, and resolve web requests using the behavior of your instance. They're the workhorse methods that make all of the earlier configuration worthwhile. So, let's take a look at each of these sets of methods in turn and fully crystalize our understanding of this class.