When using the POST method for an httpwebrequest, I often see a line of code like this:
Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("https://www.empireavenue.com/user/login/do"), HttpWebRequest)
Why do we do a DirectCast here? What is it exactly doing?
Edit: Or maybe my question is, Why do we call WebRequest.Create and cast it to an HttpWebRequest? What is going on here technically speaking?
WebRequest.Create
is a factory method which can return different types of requests. Because of that all of them are returned typed as WebRequest
. But because you may know, that you're expecting it to return HttpWebRequest
(because Uri you've provided is http) you can downcast it to get access to methods and properties exposed by HttpWebRequest
, which are not exposed by WebRequest
base class.
But, you should probably use WebRequest.CreateHttp()
instead of WebRequest.Create()
, if you know you're going to use http protocol.