'System.Net.FileWebRequest' can not be casted to 'System.Net.HttpWebRequest' on remote machine, but works locally
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