I am getting the error when trying from a server (deployed the same code on the server):
Unable to cast object of type 'System.Net.FileWebRequest' to type 'System.Net.HttpWebRequest'
But when I am trying to use the same code from my local machine, it is not giving error.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
Any idea guys what could be wrong?
The URI being passed in is not an http
URI -- it's either just a path or a file
URI. Ensure the URI starts with http:
. If it's a relative URI, you'll need to make it absolute.
WebRequest
is the type returned by WebRequest.Create()
factory method, and is an abstract type.
According to the protocol recognized in the URL string, it returns you a valid subclass, like FileWebRequest
or FtpWebRequest
.
The problem in your code is that you are trying to create a request for a local file (file://) protocol, so the factory returns FileWebRequest
, but you are forcing the code to think it's are remote HTTP URL. Simply wrong.
This explains the fact that it works only with remote and not local files
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With