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.
Afficher le fichier Open project: RickStrahl/WestwindToolkit Class Usage Examples

Méthodes publiques

Méthode 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

Méthodes protégées

Méthode Description
StreamPostBuffer ( Stream PostData ) : void

Sends the Postbuffer to the server

Private Methods

Méthode 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 méthode

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
Résultat bool

AddPostKey() public méthode

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
Résultat void

AddPostKey() public méthode

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
Résultat void

AddPostKey() public méthode

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
Résultat void

AddPostKey() public méthode

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
Résultat void

CreateWebRequestObject() public méthode

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
Résultat bool

Dispose() public méthode

Releases response and request data
public Dispose ( ) : void
Résultat void

DownloadBytes() public méthode

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
Résultat byte[]

DownloadBytesAsync() public méthode

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
Résultat Task

DownloadFile() public méthode

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
Résultat bool

DownloadResponse() public méthode

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.
Résultat HttpWebResponse

DownloadResponseAsync() public méthode

public DownloadResponseAsync ( string url ) : Task
url string
Résultat Task

DownloadStream() public méthode

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.
Résultat StreamReader

DownloadString() public méthode

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.
Résultat string

DownloadStringAsync() public méthode

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.
Résultat Task

DownloadStringPartial() public méthode

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
Résultat string

HttpClient() public méthode

The HttpClient Default Constructor
public HttpClient ( ) : System
Résultat System

ResetPostData() public méthode

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

SetPostStream() public méthode

public SetPostStream ( Stream postStream ) : void
postStream Stream
Résultat void

StreamPostBuffer() protected méthode

Sends the Postbuffer to the server
protected StreamPostBuffer ( Stream PostData ) : void
PostData Stream
Résultat void