# HttpClientRequest 包含 Get、Post、Put、Patch、Delete * ### Get ``` internal class HttpClientRequest { public static async Task<string> Get(string Url) { var client = new HttpClient(); // Get the response. HttpResponseMessage response = await client.GetAsync(Url); // Get the response content. HttpContent responseContent = response.Content; // Get the stream of the content. using (var reader = new StreamReader(await responseContent.ReadAsStreamAsync())) { return await reader.ReadToEndAsync(); } } } ``` 定義一個 HttpClientRequest 類別,其中包含一個靜態異步方法 Get。該方法接受一個 URL,使用 HttpClient 發送 HTTP GET 請求。方法會等待請求完成後,從回應中讀取數據並以字符串形式返回。 --- * ### Post ``` internal class HttpClientRequest { public static async Task<string> Post(string Url, List<KeyValuePair<string, string>> values) { var client = new HttpClient(); var packages = new FormUrlEncodedContent(values); // Get the response. HttpResponseMessage response = await client.PostAsync(Url, packages); // Get the response content. HttpContent responseContent = response.Content; // Get the stream of the content. using (var reader = new StreamReader(await responseContent.ReadAsStreamAsync())) { return await reader.ReadToEndAsync(); } } ``` 定義一個 HttpClientRequest 類別,其中包含一個靜態異步方法 Post。該方法接受 URL 和表單數據,使用 HttpClient 來發送 HTTP POST 請求。請求的內容是經過 FormUrlEncodedContent 封裝的表單數據。方法會等待請求完成後,從回應中讀取數據並以字符串形式返回。 --- * ### Put ``` internal class HttpClientRequest { public static async Task<string> Put(string Url, string jsonContent) { var client = new HttpClient(); var content = new StringContent(jsonContent, Encoding.UTF8, "application/json"); // Get the response. HttpResponseMessage response = await client.PutAsync(Url, content); // Get the response content. HttpContent responseContent = response.Content; // Get the stream of the content. using (var reader = new StreamReader(await responseContent.ReadAsStreamAsync())) { return await reader.ReadToEndAsync(); } } } ``` 定義一個 HttpClientRequest 類別,其中包含一個靜態異步方法 Put。該方法接受一個 URL 和一個 JSON 字符串,使用 HttpClient 發送 HTTP PUT 請求。請求的內容是以 JSON 格式封裝的字符串。方法會等待請求完成後,從回應中讀取數據並以字符串形式返回。 --- * ### Patch ``` internal class HttpClientRequest { public static async Task<string> Patch(string Url, string jsonContent) { var client = new HttpClient(); var content = new StringContent(jsonContent, Encoding.UTF8, "application/json"); var request = new HttpRequestMessage(new HttpMethod("PATCH"), Url) { Content = content }; // Get the response. HttpResponseMessage response = await client.SendAsync(request); // Get the response content. HttpContent responseContent = response.Content; // Get the stream of the content. using (var reader = new StreamReader(await responseContent.ReadAsStreamAsync())) { return await reader.ReadToEndAsync(); } } } ``` 定義一個 HttpClientRequest 類別,其中包含一個靜態異步方法 Patch。該方法接受一個 URL 和一個 JSON 字符串,使用 HttpClient 發送 HTTP PATCH 請求。請求的內容是以 JSON 格式封裝的字符串。方法會等待請求完成後,從回應中讀取數據並以字符串形式返回。 --- * ### Delete ``` internal class HttpClientRequest { public static async Task Delete(string Url) { var client = new HttpClient(); // Get the response. HttpResponseMessage response = await client.DeleteAsync(Url); // Get the response content. HttpContent responseContent = response.Content; // Get the stream of the content. using (var reader = new StreamReader(await responseContent.ReadAsStreamAsync())) { // Write the output. Console.WriteLine(await reader.ReadToEndAsync()); } } } ``` 定義一個 HttpClientRequest 類別,其中包含一個靜態異步方法 Delete。該方法接受一個 URL,使用 HttpClient 發送 HTTP DELETE 請求。方法會等待請求完成後,從回應中讀取數據並將其輸出到控制台。 --- ### 補充 * JSON 資料通常代表一個或多個物件的集合 API 可能返回 ``` string json = @" [ { ""lineNumber"": ""TEST"", ""lineName"": ""TEST1"" }, { ""lineNumber"": ""TEST2"", ""lineName"": ""TEST2"" } ]"; ``` * 反序列化(Deserialization) 當你使用 JsonConvert.DeserializeObject<List<你的類別>>(jsonResponse) 這樣的代碼時 JSON 字符串會被轉換為一個"你的類別"類別實例的列表 (List<你的類別>)。 * GET / POST 1. 差異 GET和POST本質上就是TCP鏈接,並無差別,但是由於HTTP的規定和瀏覽器/伺服器的限制,導致他們在應用過程中體現出一些不同 GET參數透過URL傳遞,POST放在Request body中 2. 安全 POST 比 GET安全,因為資料在網址列上不可見 但從傳輸的角度來說,他們都是不安全的,因為 HTTP 在網路上是明文傳輸的,只要在網路節點上捉包,就能完整地取得資料封包 只有使用HTTPS才能加密安全 3. 數據包 對於GET方式的請求,瀏覽器會把http header和data一併發送出去,伺服器回應200(返回資料) 對於POST,瀏覽器先發送header,伺服器回應100 continue,瀏覽器再發送data,伺服器回應200 ok 並不是所有瀏覽器都會在POST中發送兩次包,Firefox就只發送一次 <div style="text-align: center;"> <p>其實還有很多可以探討 就之後再補充吧</p> <img src="https://hackmd.io/_uploads/HkD9-jAiR.png" alt="http" /> </div>