Cannot pass an object of type "System.Net.FileWebRequest" to enter "System.Net.HttpWebRequest".

Hi guys I get the above error when trying from the server (it deploys the same code on the server). But when I try to use the same code from my local machine, it gives no errors.

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); 

Any ideas guys what could be wrong?

+4
source share
2 answers

The transmitted URI is not an http URI — it is either just a path or a file URI. Make sure the URI starts with http: If this is a relative URI, you need to make it absolute.

+7
source

WebRequest is the type returned by the WebRequest.Create() factory method and is abstract .

According to the protocol recognized in the URL string, it returns a valid subclass, such as FileWebRequest or FtpWebRequest .

The problem in your code is that you are trying to create a request for a local file (file: //), so the factory returns FileWebRequest , but you make the code think that it has removed the HTTP URL. Just wrong .

This explains the fact that it only works with remote, not local, files.

+2
source

Source: https://habr.com/ru/post/1335911/


All Articles