Class Fetch
- Namespace
- ZapLib
- Assembly
- ZapLib.dll
HTTP 網路請求與接收回應工具
public class Fetch : IDisposable
- Inheritance
-
Fetch
- Implements
- Inherited Members
Constructors
Fetch(string)
建構子,初始化必要物件
public Fetch(string uri = null)
Parameters
uristring連線的 URL
Properties
Accept
能夠接受的回應內容類型
public string Accept { get; set; }
Property Value
Client
HTTP 連線主要物件
public HttpClient Client { get; set; }
Property Value
ClientHandler
HTTP 連線資訊控制器
public HttpClientHandler ClientHandler { get; set; }
Property Value
ContentType
請求體的多媒體類型,在非 GET 請求時,它將影響資料邊碼格式
public string ContentType { get; set; }
Property Value
Cookie
HTTP Cookie
public object Cookie { get; set; }
Property Value
Header
HTTP 標頭 Header
public object Header { get; set; }
Property Value
MaxDownloadSize
方法 GetBinary() 的資源大小上限,預設 25MB
public int MaxDownloadSize { get; set; }
Property Value
Proxy
網路代理,預設為 NULL
public string Proxy { get; set; }
Property Value
Qs
查詢字串 HTTP Query String
public object Qs { get; set; }
Property Value
Request
HTTP 請求主要物件
public HttpRequestMessage Request { get; set; }
Property Value
RequestEncoding
發送請求的資料的編碼格式,預設為 UTF8
public Encoding RequestEncoding { get; set; }
Property Value
Response
HTTP 回應主要物件
public HttpResponseMessage Response { get; }
Property Value
StatusCode
HTTP 回應狀態碼,在送出請求之前一律為 0
public int StatusCode { get; }
Property Value
Url
網址 URL
public string Url { get; set; }
Property Value
ValidPlatform
是否加入平台驗證 Header 資料
public bool ValidPlatform { get; set; }
Property Value
Methods
BuildFormUrlEncodedContent(object)
將資料邊碼成 Url Encoded Form
public FormUrlEncodedContent BuildFormUrlEncodedContent(object data)
Parameters
dataobject資料
Returns
- FormUrlEncodedContent
請求的內容物件
BuildMultipartFormDataContent(object, object)
將資料邊碼成 Multipart Form
public MultipartFormDataContent BuildMultipartFormDataContent(object data = null, object files = null)
Parameters
Returns
- MultipartFormDataContent
請求的內容物件
BuildStringContent(string)
將資料邊碼成純文字格式
public StringContent BuildStringContent(string data = null)
Parameters
datastring資料
Returns
- StringContent
請求的內容物件
Delete(object, object, object)
發送 HTTP DELETE 請求,取得回傳資料後以字串回傳,請求失敗時 (200~299之外) 將回傳 NULL
public string Delete(object data = null, object qs = null, object files = null)
Parameters
Returns
- string
HTTP 回應的內容
Delete<T>(object, object, object)
發送 HTTP DELETE 請求,Accept 將設為 application/json 預期取得 JSON 資料,並將資料反序列化綁定到資料模型 T 中,請求失敗時 (200~299之外) 將回傳 NULL (如果有 file 傳送檔案,ContentType 將設為 multipart/form-data)
public T Delete<T>(object data = null, object qs = null, object files = null)
Parameters
Returns
- T
綁定資料的資料模型
Type Parameters
T指定綁定的資料型態
Dispose()
釋放 Fetch 所有使用的物件
public void Dispose()
Get(object)
發送 HTTP GET 請求,取得回傳資料後以字串回傳,請求失敗時 (200~299之外) 將回傳 NULL
public string Get(object qs = null)
Parameters
qsobject查詢字串 Query String
Returns
- string
HTTP 回應的內容
GetBinary(object)
發送 HTTP GET,直接以 byte 方式將資料讀入後回傳,請求失敗時 (200~299之外) 將回傳 NULL
public byte[] GetBinary(object qs = null)
Parameters
qsobject查詢字串
Returns
- byte[]
HTTP 回應的內容
GetBinaryResponse()
以 byte[] 方式取得發送請求後的回應結果,尚未發送請求前預設為 NULL
public byte[] GetBinaryResponse()
Returns
- byte[]
HTTP 回應的資料
GetResponse()
以純文字方式取得發送請求後的回應結果,尚未發送請求前預設為 NULL
public string GetResponse()
Returns
- string
HTTP 回應的資料
GetResponseHeader(string)
取得發送請求後指定 key 名稱的 HTTP Header 資料,如果找不到該資料將回傳 NULL
public string GetResponseHeader(string key)
Parameters
keystringHeader 名稱
Returns
- string
HTTP 回應的特定標頭數值
GetResponseHeader<T>(string, T)
取得發送請求後指定 key 名稱的 HTTP Header 資料,並自動轉型成指定型態,如果找不到該資料將回傳第二個參數 default
public T GetResponseHeader<T>(string key, T defaultVal = default)
Parameters
keystringHeader 名稱
defaultValT預設的數值
Returns
- T
HTTP 回應的特定標頭數值
Type Parameters
T
GetResponseHeaders()
取得發送請求後的所有 HTTP Header 資料
public HttpResponseHeaders GetResponseHeaders()
Returns
- HttpResponseHeaders
HTTP 所有回應的標頭
Get<T>(object)
發送 HTTP GET 請求,Accept 將設為 application/json 預期取得 JSON 資料,並將資料反序列化綁定到資料模型 T 中,請求失敗時 (200~299之外) 將回傳 NULL
public T Get<T>(object qs = null)
Parameters
qsobject查詢字串
Returns
- T
綁定資料的資料模型
Type Parameters
T指定綁定的資料型態 T
Patch(object, object, object)
發送 HTTP Patch 請求,取得回傳資料後以字串回傳,請求失敗時 (200~299之外) 將回傳 NULL (如果有 file 傳送檔案,ContentType 將設為 multipart/form-data)
public string Patch(object data = null, object qs = null, object files = null)
Parameters
Returns
- string
HTTP 回應的內容
Patch<T>(object, object, object)
發送 HTTP Patch 請求,Accept 將設為 application/json 預期取得 JSON 資料,並將資料反序列化綁定到資料模型 T 中,請求失敗時 (200~299之外) 將回傳 NULL (如果有 file 傳送檔案,ContentType 將設為 multipart/form-data)
public T Patch<T>(object data = null, object qs = null, object files = null)
Parameters
Returns
- T
綁定資料的資料模型
Type Parameters
T指定綁定的資料型態
Post(object, object, object)
發送 HTTP POST 請求,取得回傳資料後以字串回傳,請求失敗時 (200~299之外) 將回傳 NULL (如果有 file 傳送檔案,ContentType 將設為 multipart/form-data)
public string Post(object data = null, object qs = null, object files = null)
Parameters
Returns
- string
HTTP 回應的內容
Post<T>(object, object, object)
發送 HTTP POST 請求,Accept 將設為 application/json 預期取得 JSON 資料,並將資料反序列化綁定到資料模型 T 中,請求失敗時 (200~299之外) 將回傳 NULL (如果有 file 傳送檔案,ContentType 將設為 multipart/form-data)
public T Post<T>(object data = null, object qs = null, object files = null)
Parameters
Returns
- T
綁定資料的資料模型
Type Parameters
T指定綁定的資料型態
Put(object, object, object)
發送 HTTP PUT 請求,取得回傳資料後以字串回傳,請求失敗時 (200~299之外) 將回傳 NULL
public string Put(object data = null, object qs = null, object files = null)
Parameters
Returns
- string
HTTP 回應的內容
Put<T>(object, object, object)
發送 HTTP PUT 請求,Accept 將設為 application/json 預期取得 JSON 資料,並將資料反序列化綁定到資料模型 T 中,請求失敗時 (200~299之外) 將回傳 NULL (如果有 file 傳送檔案,ContentType 將設為 multipart/form-data)
public T Put<T>(object data = null, object qs = null, object files = null)
Parameters
Returns
- T
綁定資料的資料模型
Type Parameters
T指定綁定的資料型態
Send()
手動送出請求 (注意:每個 Fetch 物件只能發送一次請求)
public bool Send()
Returns
SetRequestContent(object, object)
設定 Request.Content 的資料
public void SetRequestContent(object data = null, object files = null)
Parameters
SetRequestContnet(object, object)
[已棄用] 設定 Request.Content 的資料。請改用 SetRequestContent(object, object)
[Obsolete("方法已重新命名為 SetRequestContent,原拼字錯誤的版本將於下一個 major 版本移除")]
public void SetRequestContnet(object data = null, object files = null)