System.Net.WebClient

Added in .NET Framework 1.1
This is derived from the System.Net.WebRequest class.
This class is a higher-level abstraction built on top of HttpWebRequest to simplify the most common tasks
Very basic method and uses simple GET requests


Create

string templateName = "templateName"; 
string returnText = "";

string restURL = "https:\\bettersolutions.com\

var request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(restURL);
request.AutomaticDecompression = System.Net.DecompressionMethods.GZip;
request.Method = "GET";
request.ContentType = "application/json";
request.CookieContainer = new System.Net.CookieContainer();
request.AllowAutoRedirect = true;
request.UseDefaultCredentials = true;
request.Timeout = (int)TimeSpan.FromMinutes(1).TotalMilliseconds;

using (var reader = new System.IO.StreamReader(request.GetResponse().GetResponseStream(), System.Text.Encoding.UTF8, true))
{
     System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
     var objText = reader.ReadToEnd();
     returnValue = js.Deserialize(objText, typeof(ModelCoverProject.DiscTextJsonResponse));
}

OpenRead

Dim objStream As System.IO.Stream 
Dim objStreamReader As System.IO.StreamReader
Dim oWebClient As System.Net.WebClient

objWebClient = New System.Net.WebClient
objWebClient.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials
objWebClient.Credentials = New System.Net.NetworkCredential(username, password, domain)

objStream - objWebClient.OpenRead(sURL)
objStreamReader = New System.IO.StreamReader(objStream)

This method copies the existing Headers, Credentials, and method to the newly created WebRequest object.

public static void Main (string[] args) 
{
     System.Net.WebClient oWebClient = new System.Net.WebClient();
     oWebClient.Headers.Add ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
     Stream data = oWebClient.OpenRead (args[0]);

     StreamReader reader = new StreamReader (data);
    string s = reader.ReadToEnd ();
    System.Console.WriteLine (s);
    data.Close ();
    reader.Close ();
}

DownloadString

public static string HttpGet(string uri) 
{
    string content = null;
    System.Net.WebClient oWebClient;
 
    oWebClient = new System.Net.WebClient();
    content = oWebClient.DownloadString(uri);
 
    return content;
}

UploadFile

Dim oWebClient As System.Net.WebClient; 
oWebClient = New System.Net.WebClient();

oWebClient.UploadFile("https://bettersolutions.com/uploadfile/", "POST", "C:\temp\myfile.xls.gz");

Methods

OpenWriteUploading data
DownloadDataDownloading data - returns a byte string
DownloadDataAsync(Added in 2.0)
DownloadFileDownloading data - saves it to a local file
DownloadFileAsync(Added in 2.0)
DownloadStringDownloading data
DownloadStringAsync(Added in 2.0)
GetWebRequest 
GetWebResponse 
OpenReadDownloading data - returning a stream
OpenReadAsync(Added in 2.0)
OpenWriteAsync 
UploadDataUploading data
UploadFileUploading data
UploadString 
UploadStringAsync(Added in 2.0)
UploadValuesUploading data
UploadValuesAsync(Added in 2.0)

Properties

AllowReadStreamBuffering 
AllowWriteStreamBuffering 
BaseAddress 
CachePolicy 
Credentials 
Encoding 
Headers 
IsBusy 
Proxy 
QueryString 
ResponseHeaders 
UseDefaultCredentials 

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext