Class HttpClient

An HTTP wrapper class that abstracts away the common needs for adding post keys and firing update events as data is received. This class is real easy to use with many common operations requiring single method calls.

The class also provides automated cookie and state handling, GZip compression decompression, simplified proxy and authentication mechanisms to provide a simple single level class interface. The underlying WebRequest is also exposed so you will not loose any functionality from the .NET BCL class.

public class HttpClient : object, IDisposable

Class Members



The HttpClient Default Constructor


Fires progress events when receiving data from the server


Fires progress events when using GetUrlEvents() to retrieve a URL.


Allows posting a file to the Web Server. Make sure that you set PostMode

public bool AddPostFile(string key,     string fileName,     string contentType)


Adds POST form variables to the request buffer. PostMode determines how parms are handled.

public void AddPostKey(string key,     Byte[] value)

public void AddPostKey(string key,     string value)

public void AddPostKey(string fullPostBuffer)

public void AddPostKey(Byte[] fullPostBuffer)


public bool CreateWebRequestObject(string Url)


Retrieves URL into an Byte Array.

public Byte[] DownloadBytes(string url,     long bufferSize)


Retrieves URL into an Byte Array.

public Task<Byte[]> DownloadBytesAsync(string url,     long bufferSize)


Writes the output from the URL request to a file firing events.

public bool DownloadFile(string url,     long bufferSize,     string outputFile)


Return an HttpWebResponse object for a request. You can use the Response to read the result as needed. This is a low level method. Most of the other 'Get' methods call this method and process the results further.

public HttpWebResponse DownloadResponse(string url)


public Task DownloadResponseAsync(string url)


Return a the result from an HTTP Url into a StreamReader. Client code should call Close() on the returned object when done reading.

public StreamReader DownloadStream(string url)


Returns the content of a URL as a string using a specified Encoding

public string DownloadString(string url,     long bufferSize,     Encoding encoding)


Returns the content of a URL as a string using a specified Encoding

public Task DownloadStringAsync(string url,     long bufferSize,     Encoding encoding)


Returns a partial response from the URL by specifying only given number of bytes to retrieve. This can reduce network traffic and keep string formatting down if you are only interested a small port at the top of the page. Also returns full headers.

public string DownloadStringPartial(string url,     int size)


Returns the contents of the post buffer. Useful for debugging

public string GetPostBuffer()


Resets the Post buffer by clearing out all existing content

public void ResetPostData()


public void SetPostStream(Stream postStream)


The buffersize used for the Send and Receive operations


Returns whether the last request was cancelled through one of the events.


Use this option to set a custom content type. If possible use PostMode to specify a predefined content type as it will ensure that Post data is appropriately formatted.

If setting the content type manually POST data


Holds the internal Cookie collection before or after a request. This collection is used only if HandleCookies is set to .t. which also causes it to capture cookies and repost them on the next request.


Error flag if an error occurred.


Error Message if the Error Flag is set or an error value is returned from a method.


If set to true will automatically track cookies between multiple successive requests on this instance. Uses the CookieCollection property to persist cookie status.

When set posts values in the CookieCollection, and on return fills the CookieCollection with cookies from the Response.


Keeps track of request timings for the last executed request. Tracks started, firstbyte and lastbyte times as well as ms to first byte (actually first 'buffer' loaded) and last byte.



Sets the certificate policy.

Note this is a global setting and affects the entire application. It's recommended you set this for the application and not on a per request basis.


Password for Authentication.


Determines how data is POSTed when when using AddPostKey() and other methods of posting data to the server. Support UrlEncoded, Multi-Part, XML and Raw modes.


Address of the Proxy Server to be used. Use optional DEFAULTPROXY value to specify that you want to IE's Proxy Settings


Semicolon separated Address list of the servers the proxy is not used for.


Password for a password validating Proxy. Only used if the proxy info is set.


Username for a password validating Proxy. Only used if the proxy info is set.


Determines whether errors cause exceptions to be thrown. By default errors are handled in the class and the Error property is set for error conditions. (not implemented at this time).


Timeout for the Web request in seconds. Times out on connection, read and send operations. Default is 30 seconds.


When true will automatically add Accept-Encoding: gzip,deflate header and automatically decompress gzip and deflate content


Lets you specify the User Agent browser string that is sent to the server. This allows you to simulate a specific browser if necessary.


User name used for Authentication. To use the currently logged in user when accessing an NTLM resource you can use "AUTOLOGIN".


WebRequest object that can be manipulated and set up for the request if you called .

Note: This object must be recreated and reset for each request, since a request's life time is tied to a single request. This object is not used if you specify a URL on any of the GetUrl methods since this causes a default WebRequest to be created.



Namespace: Westwind.Utilities.InternetTools
Assembly: westwind.utilities.dll

© West Wind Technologies, 2019 • Updated: 06/23/19
Comment or report problem with topic