HTTP請求的基本格式
Windows中使用\r\n表示下一行,MAC使用\r,Unix使用\n
{
HTTP響應的基本格式
第一行都會是狀態行,內容是 HTTP版本號、3位數字的狀態碼、狀態短語
{response-body} 以上三個與請求相同含義
HttpWebRequest 用法
C# HttpWebRequest詳解_PassionY的部落格-CSDN部落格_c# httpwebrequest
裡面有案例,自我感覺不太適用初學,用法其實就是列出裡面屬性和方法的解釋
HttpWebRequest 用法
C# 之 HttpWebResponse類 - 走看看
Http請求與響應 示例
使用窗體寫的,執行截圖
HTML
Headers
Get下拉框是 http的請求方式,常用的有GET、POST、PUT、DELETE
Http請求方式的正確使用場景_只是六號z的部落格-CSDN部落格_http的應用場景
大概的對應關係 GET —— 查,POST —— 增,PUT —— 改,DELETE —— 刪
後面的 textbox 輸入 uri地址
Send 傳送請求
下面的 textbox 展示響應或者請求的資訊
核心程式碼
HttpWebRequest Req;HttpWebResponse Res;private string uri;private string Method;string str;private void Send_Click(object sender, EventArgs e) { if (string。IsNullOrEmpty(textBox1。Text)) uri = $“{comboBox2。Text}{textBox1。PlaceholderText}”; else uri = $“{comboBox2。Text}{textBox1。Text}”; Method = comboBox1。Text;#pragma warning disable SYSLIB0014 // 型別或成員已過時 // 建立請求 Req = (HttpWebRequest)WebRequest。Create(new Uri(uri));#pragma warning restore SYSLIB0014 // 型別或成員已過時 // 設定請求的方法 Req。Method = Method; // 設定請求超時時間 Req。Timeout = 5000; // 設定是否對傳送到 Internet 資源的資料進行緩衝處理 Req。AllowWriteStreamBuffering = false; // 設定HTTP內容型別,底下參考有連線 Req。ContentType = “application/x-www-form-urlencoded”; // 設定HTTP 版本 Req。ProtocolVersion = new Version(1, 1); if(Method。ToString() == “POST”) // 傳送 SendPost(); else // 獲取 請求 返回的 響應 Res = (HttpWebResponse)Req。GetResponse(); // 獲取響應資料 using (StreamReader sr = new StreamReader(Res。GetResponseStream())) { str = sr。ReadToEnd(); } }private void HTML_Click(object sender, EventArgs e) { // 將獲取到的響應放到 TextBox textBox2。Text = str; }private void Headers_Click(object sender, EventArgs e) { // 將獲取到的響應放到 TextBox textBox2。Text = $“Headers: \r\n { Res。Headers } \nMethod(請求方法): { Req。Method } \r\nReqUri(請求地址): { Req。RequestUri }\r\nResUri(響應地址): { Res。ResponseUri }”; }private void SendPost() { // post 請求提交的資料 Dictionary
參考:
1、C#基礎之HTTP入門(一)——HTTP基礎知識_Jeffrey_Chou的部落格-CSDN部落格_c# http
2、headers資訊-online tool-postjson
3、http狀態碼及短語 -online tool-postjson
4、ContextType 查詢 -online tool-postjson