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.Createis a factory method which can return different types of requests. Because of that all of them are returned typed asWebRequest. But because you may know, that you're expecting it to returnHttpWebRequest(because Uri you've provided is http) you can downcast it to get access to methods and properties exposed byHttpWebRequest, which are not exposed byWebRequestbase class.But, you should probably use
WebRequest.CreateHttp()instead ofWebRequest.Create(), if you know you're going to use http protocol.