John Odom John Odom - 19 days ago 11
C# Question

Difference between WebRequest.Create and WebRequest.CreateHttp

I am looking at the documentations between

and
WebRequest.CreateHttp
to decide which I should use in my application. I do not see anything different between these two methods except
WebRequest.Create
returns a
WebRequest
and
WebRequest.CreateHttp
returns
HttpWebRequest
.

Are these two methods essentially the same except for the return value? Or do they each do something different internally?

Answer

WebRequest.Create will return a WebRequest-derived request, based on the URL you use. If,for example, you use an FTP URL, the result will be a FileWebRequest. CreateHttp on the other hand creates an HttpWebRequest explicitly.

CreateHttp was added in .Net 4.0 while Create itself exists since 2.0.

Both methods will create an HttpWebRequest so they can be used interchangeably.

Comments