WebRequest replacement

The Rebex.Net.HttpRequest class is a replacement for System.Net.HttpWebRequest class. It can either be used stand-alone, or plugged into .NET Frameworks WebRequest/WebResponse infrastructure. For a list of common pitfalls, check out Migrating to HttpRequest article.

Stand-alone usage #

To utilize the enhanced abilities of HttpRequest without affecting the rest of your application in any way, use HttpRequest as a stand-alone object. It's simple - instead of using .NET's WebRequest.Create use our HttpRequestCreator to create your WebRequest objects.

CSharp

// instantiate HttpRequest creator
var creator = new HttpRequestCreator();

// specify any settings if needed
creator.Settings.SslAllowedVersions = TlsVersion.TLS12 | TlsVersion.TLS11 | TlsVersion.TLS10;

// use the creator instead of WebRequest.Create to get your HTTP request
WebRequest request = creator.Create(uri);

// the request object is an instance of HttpRequest (not HttpWebRequest),
// which means you can do this as well to access HTTP-specific parts of it:
//HttpRequest request = creator.Create(uri);

// get the response for the request
WebResponse response = request.GetResponse();
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
    // read the response and process it
    string body = sr.ReadToEnd();
    Process(body);
}

response.Close();

VisualBasic

' instantiate HttpRequest creator
Dim creator = New HttpRequestCreator()

' specify any settings if needed
creator.Settings.SslAllowedVersions = TlsVersion.TLS12 Or TlsVersion.TLS11 Or TlsVersion.TLS10

' use the creator instead of WebRequest.Create to get your HTTP request
Dim request As WebRequest = creator.Create(uri)

' the request object is an instance of HttpRequest (not HttpWebRequest),
' which means you can do this as well to access HTTP-specific parts of it:
'HttpRequest request = creator.Create(uri);

' get the response for the request
Dim response As WebResponse = request.GetResponse()
Using sr As New StreamReader(response.GetResponseStream())
    ' read the response and process it
    Dim body As String = sr.ReadToEnd()
    Process(body)
End Using

response.Close()

Plugging into .NET WebRequest factory #

Registering HttpRequestCreator with .NET's WebRequest is another option. This affects the entire application and makes WebRequest use Rebex HttpRequest instead of System.Net.HttpWebRequest to handle HTTP and HTTPS requests.

CSharp

// instantiate HttpRequest creator
var creator = new HttpRequestCreator();

// specify any settings if needed
creator.Settings.SslAllowedVersions = TlsVersion.TLS12 | TlsVersion.TLS11 | TlsVersion.TLS10;

// register this creator with .NET's WebRequest to handle HTTP
// and HTTPS requests (replacing System.Net.HttpWebRequest)
creator.Register();

// .NET's WebRequest.Create uses Rebex HTTPS now
WebRequest request = WebRequest.Create(uri);

// the request object is an instance of HttpRequest (not HttpWebRequest),
// which means you can do this as well to access HTTP-specific parts of it:
//HttpRequest request = (HttpRequest)WebRequest.Create(uri);

// get the response for the request
WebResponse response = request.GetResponse();
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
    // read the response and process it
    string body = sr.ReadToEnd();
    Process(body);
}

response.Close();

VisualBasic

' instantiate HttpRequest creator
Dim creator = New HttpRequestCreator()

' specify any settings if needed
creator.Settings.SslAllowedVersions = TlsVersion.TLS12 Or TlsVersion.TLS11 Or TlsVersion.TLS10

' register this creator with .NET's WebRequest to handle HTTP
' and HTTPS requests (replacing System.Net.HttpWebRequest)
creator.Register()

' .NET's WebRequest.Create uses Rebex HTTPS now
Dim request As WebRequest = WebRequest.Create(uri)

' the request object is an instance of HttpRequest (not HttpWebRequest),
' which means you can do this as well to access HTTP-specific parts of it:
'HttpRequest request = (HttpRequest)WebRequest.Create(uri);

' get the response for the request
Dim response As WebResponse = request.GetResponse()
Using sr As New StreamReader(response.GetResponseStream())
    ' read the response and process it
    Dim body As String = sr.ReadToEnd()
    Process(body)
End Using

response.Close()

Authentication #

To authenticate to an HTTP server, just set the Credentials property to an instance of the System.Net.NetworkCredential. The authentication method is chosen automatically (from authentication methods supported by the HTTP server) in the following order:

  • Negotiate (not supported on Mono, Android and iOS)
  • NTLM
  • Kerberos v5 (not supported on Mono, Android and iOS)
  • Basic

CSharp

// assign credentials to the request
request.Credentials = new System.Net.NetworkCredential(userName, password);

VisualBasic

' assign credentials to the request
request.Credentials = New System.Net.NetworkCredential(userName, password)

If the UserName is not specified, single sign-on is performed.

Note: Single sign-on is only supported on Windows platforms.

CSharp

// assign empty credentials to issue single sign-on authentication
request.Credentials = new System.Net.NetworkCredential();

VisualBasic

' assign empty credentials to issue single sign-on authentication
request.Credentials = New System.Net.NetworkCredential()

Automatic connection initiation (.NET CF only) #

.NET Compact Framework edition of Rebex HTTPS supports automated connection initiation. However, unlike .NET's WebRequest, this feature is not enabled by default. To enable it, set HttpRequestCreator's Settings.AutoConnectToInternet to Rebex.Net.ConnectionManagement.AutoConnectType.Enabled.