WebClient replacement

The Rebex.Net.WebClient class is indended to be a replacement for System.Net.WebClient class. However, unlike HttpRequest, it's not a one-to-one replacement. Some features such as event-based asynchronous API have not been included, and progress-reporting events use a slightly different approach.

Downloading files #

To download a file, use one of the Download* methods:

CSharp

// create an instance of WebClient
var client = new Rebex.Net.WebClient();

// downloading into a file
client.DownloadFile("https://test.rebex.net/rebex-logo.gif", "rebex.gif");

// downloading as byte array
byte[] content = client.DownloadData("https://test.rebex.net/rebex-logo.gif");

// download as string
string text = client.DownloadString("https://www.rebex.net/products/");

VisualBasic

' create an instance of WebClient
Dim client = New Rebex.Net.WebClient()

' downloading into a file
client.DownloadFile("https://test.rebex.net/rebex-logo.gif", "rebex.gif")

' downloading as byte array
Dim content As Byte() = client.DownloadData("https://test.rebex.net/rebex-logo.gif")

' download as string
Dim text As String = client.DownloadString("https://www.rebex.net/products/")

Posting requests #

Posting various requests is straightforward as well:

CSharp

// create an instance of WebClient
var client = new Rebex.Net.WebClient();

// submit a form using POST method
var values = new NameValueCollection();
values["id"] = "1234";
values["command"] = "update";
values["name"] = "Rebex";
result = client.UploadValues("https://httpbin.org/post", values);

// upload a file using POST method
result = client.UploadFile("https://httpbin.org/post", "sample.txt");

// upload a string using POST method
result = client.UploadString("https://httpbin.org/post", "Sample content");

// upload byte array using POST method
byte[] data = File.ReadAllBytes("sample.txt");
result = client.UploadData("https://httpbin.org/post", data);

// upload a byte array using PUT method instead of POST
result = client.UploadData("https://httpbin.org/put", "PUT", data);

VisualBasic

' create an instance of WebClient
Dim client = New Rebex.Net.WebClient()

' submit a form using POST method
Dim values = New NameValueCollection()
values("id") = "1234"
values("command") = "update"
values("name") = "Rebex"
result = client.UploadValues("https://httpbin.org/post", values)

' upload a file using POST method
result = client.UploadFile("https://httpbin.org/post", "sample.txt")

' upload a string using POST method
result = client.UploadString("https://httpbin.org/post", "Sample content")

' upload byte array using POST method
Dim data As Byte() = File.ReadAllBytes("sample.txt")
result = client.UploadData("https://httpbin.org/post", data)

' upload a byte array using PUT method instead of POST
result = client.UploadData("https://httpbin.org/put", "PUT", data)

Stream-based API #

If you prefer stream-based API, use OpenRead or OpenWrite methods:

CSharp

// create an instance of WebClient
var client = new Rebex.Net.WebClient();

// open a remote file as stream
using (Stream input = client.OpenRead("https://test.rebex.net/rebex-logo.gif"))
{
    // load image from the stream
    Image image = Image.FromStream(input);

    // ...
    Process(image);
}

VisualBasic

' create an instance of WebClient
Dim client = New Rebex.Net.WebClient()

' open a remote file as stream
Using input As Stream = client.OpenRead("https://test.rebex.net/rebex-logo.gif")
    ' load image from the stream
    Dim img As Image = Image.FromStream(input)

    ' ...
    Process(img)
End Using

Setting and sharing options #

WebClient object makes it possible to specify various options and parameters:

CSharp

// create an instance of WebClient
var client = new Rebex.Net.WebClient();

// set authentication credentials
client.Credentials = new NetworkCredential("user01", "password");

// set custom header
client.Headers["X-Custom-Token"] = "Tm90aGluZyBpbnRlcmVzdGluZyBoZXJl";

// submit a form using POST method
var values = new NameValueCollection();
values["id"] = "7";
values["type"] = "any";
var result = client.UploadValues("https://httpbin.org/post", values);

VisualBasic

' create an instance of WebClient
Dim client = New Rebex.Net.WebClient()

' set authentication credentials
client.Credentials = New NetworkCredential("user01", "password")

' set custom header
client.Headers("X-Custom-Token") = "Tm90aGluZyBpbnRlcmVzdGluZyBoZXJl"

' submit a form using POST method
Dim values = New NameValueCollection()
values("id") = "7"
values("type") = "any"
Dim result = client.UploadValues("https://httpbin.org/post", values)