Rebex HTTPS
HTTP and HTTPS library for modern and legacy platforms
Download 30-day free trial Buy from $349More .NET libraries
-
Rebex Total Pack
All Rebex .NET libraries together
Back to feature list...
WebClient replacement
The Rebex.Net.WebClient
class is intended 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:
// 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/");
' 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:
// 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);
' 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:
// 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); }
' 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:
// 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);
' 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)
Back to feature list...