Rebex HTTP problem with other libraries

0 votes
asked Apr 6 by Gerhard Koell (160 points)
edited Apr 6 by Gerhard Koell

Hi
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();
creator.Register();

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
answered Apr 6 by Lukas Pokorny (94,270 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.

commented Apr 6 by Gerhard Koell (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
commented Apr 9 by Lukas Pokorny (94,270 points)
edited Jun 29 by Lukas Matyska
UPDATE: the ClientCertificates collection was added in release 2018 R2.

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.
...