0 votes
by (160 points)
edited by

I have a problem using Rebex HTTP in combination with other libraries, eg RestSharp

after registering the creator like your sample:

var creator = new HttpRequestCreator();

I got an error on each request with RestSharp (eg RestClient.Execute):

could not cast "Rebex.Net.HttpRequest" to "System.Net.HttpWebRequest"

Is there a workaround to solve this problem? or are other libraries not compatible your extension?

Regards Gerhard

1 Answer

+1 vote
by (141k points)

This is a known issue with libraries which assume that System.Net.WebRequest.Create(Uri) always returns an instance of System.Net.HttpWebRequest:

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

However, after registering Rebex HttpRequestCreator, WebRequest.Create returns instances of Rebex.Net.HttpRequest which inherit from System.Net.WebRequest (not from System.Net.HttpWebRequest), which means the code above would fail.

Unfortunately, there is not much we can do about this. HttpRequest does not inherit from HttpWebRequest because it did not have a suitable constructor until .NET 4.5, and even that is not intended to actually be used by third-party code and produces an error on compilation. Using the protected serialization constructor doesn't seem quite right either because it has been made obsolete in .NET 2.0 already.

So the only reasonable solution at the moment is to modify third-party libraries to accept instances of System.Net.WebRequest as well (or modify them to use Rebex HttpRequest instead of .NET's HttpWebRequest). Unfortunately, in case of closed-source third-party libraries, that persuading the vendor to make the library compatible with System.Net.WebRequest might be a challenge.

by (160 points)
Thanks for the reply. I have replace the HttpWebRequest and responses with the rebex one. It looks good, only the cookies and ClientCertificates are not supported or must be implemented different.
thx for help
by (141k points)
edited by
Update: ClientCertificates collection was added in release 2018 R2 (https://www.rebex.net/https/history.aspx#2018R2)

We plan to add support for both HttpWebRequest-style Cookies and ClientCertificates soon. In the meantime, cookies have to be set/retrieved using custom code (use request/response Set-Cookie header - https://en.wikipedia.org/wiki/HTTP_cookie#Implementation). And client certificate authentication is supported using a different API - see https://www.rebex.net/https/features/tls-ssl.aspx#client-certificate for details.
by (69.1k points)
Update: Support for Cookies was added in release 2020 R4 (check https://www.rebex.net/total-pack/history.aspx#2020R4)