C# Class Westwind.Utilities.InternetTools.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.
Mostra file Open project: RickStrahl/WestwindToolkit Class Usage Examples

Public Methods

Method Description
AddPostFile ( string key, string fileName ) : bool

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

AddPostKey ( byte fullPostBuffer ) : void

Adds a fully self contained POST buffer to the request. Works for XML or previously encoded content.

AddPostKey ( string fullPostBuffer ) : void

Adds a fully self contained POST buffer to the request. Works for XML or previously encoded content.

AddPostKey ( string key, byte value ) : void

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

AddPostKey ( string key, string value ) : void

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

CreateWebRequestObject ( string Url ) : bool

Creates a new WebRequest instance that can be set prior to calling the various Get methods. You can then manipulate the WebRequest property, to custom configure the request. Instead of passing a URL you can then pass null. Note - You need a new Web Request for each and every request so you need to set this object for every call if you manually customize it.

Dispose ( ) : void

Releases response and request data

DownloadBytes ( string url, long bufferSize = 8192 ) : byte[]

Retrieves URL into an Byte Array.

Fires the ReceiveData Event

DownloadBytesAsync ( string url, long bufferSize = 8192 ) : Task

Retrieves URL into an Byte Array.

Fires the ReceiveData Event

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

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

DownloadResponse ( string url ) : HttpWebResponse

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.

Important: The Response object's Close() method must be called when you are done with the object.

DownloadResponseAsync ( string url ) : Task
DownloadStream ( string url ) : StreamReader

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

DownloadString ( string url, long bufferSize = 8192, Encoding encoding = null ) : string

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

DownloadStringAsync ( string url, long bufferSize = 8192, Encoding encoding = null ) : Task

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

DownloadStringPartial ( string url, int size ) : string

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.

HttpClient ( ) : System

The HttpClient Default Constructor

ResetPostData ( ) : void

Resets the Post buffer by clearing out all existing content

SetPostStream ( Stream postStream ) : void

Protected Methods

Method Description
StreamPostBuffer ( Stream PostData ) : void

Sends the Postbuffer to the server

Private Methods

Method Description
CheckCertificateCallback ( object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors errors ) : bool

Handles the Certificate check

GetUrl ( string url, long bufferSize = 8192, Encoding encoding = null ) : string
GetUrlBytes ( string url, long bufferSize = 8192 ) : byte[]
GetUrlFile ( string Url, long BufferSize, string OutputFile ) : bool
GetUrlPartial ( string url, int size ) : string
GetUrlResponse ( string url ) : HttpWebResponse
GetUrlStream ( string Url ) : StreamReader

Method Details

AddPostFile() public method

Allows posting a file to the Web Server. Make sure that you set PostMode
public AddPostFile ( string key, string fileName ) : bool
key string
fileName string
return bool

AddPostKey() public method

Adds a fully self contained POST buffer to the request. Works for XML or previously encoded content.
public AddPostKey ( byte fullPostBuffer ) : void
fullPostBuffer byte Byte array of a full POST buffer
return void

AddPostKey() public method

Adds a fully self contained POST buffer to the request. Works for XML or previously encoded content.
public AddPostKey ( string fullPostBuffer ) : void
fullPostBuffer string String based full POST buffer
return void

AddPostKey() public method

Adds POST form variables to the request buffer. PostMode determines how parms are handled.
public AddPostKey ( string key, byte value ) : void
key string Key value or raw buffer depending on post type
value byte Value to store. Used only in key/value pair modes
return void

AddPostKey() public method

Adds POST form variables to the request buffer. PostMode determines how parms are handled.
public AddPostKey ( string key, string value ) : void
key string Key value or raw buffer depending on post type
value string Value to store. Used only in key/value pair modes
return void

CreateWebRequestObject() public method

Creates a new WebRequest instance that can be set prior to calling the various Get methods. You can then manipulate the WebRequest property, to custom configure the request. Instead of passing a URL you can then pass null. Note - You need a new Web Request for each and every request so you need to set this object for every call if you manually customize it.
public CreateWebRequestObject ( string Url ) : bool
Url string
return bool

Dispose() public method

Releases response and request data
public Dispose ( ) : void
return void

DownloadBytes() public method

Retrieves URL into an Byte Array.
Fires the ReceiveData Event
public DownloadBytes ( string url, long bufferSize = 8192 ) : byte[]
url string Url to read
bufferSize long Size of the buffer for each read. 0 = 8192
return byte[]

DownloadBytesAsync() public method

Retrieves URL into an Byte Array.
Fires the ReceiveData Event
public DownloadBytesAsync ( string url, long bufferSize = 8192 ) : Task
url string Url to read
bufferSize long Size of the buffer for each read. 0 = 8192
return Task

DownloadFile() public method

Writes the output from the URL request to a file firing events.
public DownloadFile ( string url, long bufferSize, string outputFile ) : bool
url string Url to fire
bufferSize long Buffersize - how often to fire events
outputFile string File to write response to
return bool

DownloadResponse() public method

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.
Important: The Response object's Close() method must be called when you are done with the object.
public DownloadResponse ( string url ) : HttpWebResponse
url string Url to retrieve.
return HttpWebResponse

DownloadResponseAsync() public method

public DownloadResponseAsync ( string url ) : Task
url string
return Task

DownloadStream() public method

Return a the result from an HTTP Url into a StreamReader. Client code should call Close() on the returned object when done reading.
public DownloadStream ( string url ) : StreamReader
url string Url to retrieve.
return StreamReader

DownloadString() public method

Returns the content of a URL as a string using a specified Encoding
public DownloadString ( string url, long bufferSize = 8192, Encoding encoding = null ) : string
url string
bufferSize long Internal download buffer size used to hold data chunks.
encoding Encoding A .NET Encoding scheme or null to attempt sniffing from Charset.
return string

DownloadStringAsync() public method

Returns the content of a URL as a string using a specified Encoding
public DownloadStringAsync ( string url, long bufferSize = 8192, Encoding encoding = null ) : Task
url string
bufferSize long Internal download buffer size used to hold data chunks.
encoding Encoding A .NET Encoding scheme or null to attempt sniffing from Charset.
return Task

DownloadStringPartial() public method

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 DownloadStringPartial ( string url, int size ) : string
url string
size int
return string

HttpClient() public method

The HttpClient Default Constructor
public HttpClient ( ) : System
return System

ResetPostData() public method

Resets the Post buffer by clearing out all existing content
public ResetPostData ( ) : void
return void

SetPostStream() public method

public SetPostStream ( Stream postStream ) : void
postStream Stream
return void

StreamPostBuffer() protected method

Sends the Postbuffer to the server
protected StreamPostBuffer ( Stream PostData ) : void
PostData Stream
return void