C# 呼叫WebApi
阿新 • • 發佈:2020-10-10
學習筆記:
1,WebRequest方式 Get請求
#region 方法3 .WebRequest方式 Get請求 public static string HttpGet(string url) { //ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); Encoding encoding = Encoding.UTF8; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; request.Accept = "text/html, application/xhtml+xml, */*"; request.ContentType = "application/json"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { return reader.ReadToEnd(); } } private void button3_Click(object sender, EventArgs e) { //string ss = HttpGet("http://localhost:55837/api/product"); string ss = HttpGet(txt_get_url.Text); this.textBox3.Text += ss; } #endregion
2,WebRequest方式 Get帶引數請求
#region WebRequest方式 Get帶引數請求 public static string HttpGet6(string url) { //ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); Encoding encoding = Encoding.UTF8; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; request.Accept = "text/html, application/xhtml+xml, */*"; request.ContentType = "application/json"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { return reader.ReadToEnd(); } } private void button6_Click(object sender, EventArgs e) { string id = this.txt_get_parameter.Text; string ss = HttpGet6(txt_get_url.Text+ "/"+id.Trim()); this.textBox6.Text += ss; } #endregion
3,WebRequest方式 Post請求
"application/x-www-form-urlencoded";//引數:username=admin&password=123 如果引數是json格式或者引數寫錯不會報錯的 byte[] buffer = encoding.GetBytes(body); request.ContentLength = buffer.Length; request.GetRequestStream().Write(buffer, 0, buffer.Length); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { return reader.ReadToEnd(); } } private void button4_Click(object sender, EventArgs e) { string url = this.txt_post_url.Text; string body = this.txt_post_parameter.Text; string ss = HttpWebRequestPost(url, body); this.textBox4.Text += ss; } #endregion
4,HttpClient 方式 Get請求
#region 方法4 HttpClient 方式 Get請求 public static string HttpClienGet(string url) { HttpClient hc = new HttpClient(); string html= hc.GetStringAsync(url).Result; return html; } private void btn_HttpClentGet_Click(object sender, EventArgs e) { string url = this.txt_get_url.Text; string html = HttpClienGet(url); txt_HttpClentGet_Result.Text += html; } #endregion
5,HttpClient 方式 Post請求
#region 方法4 HttpClient 方式 Post請求 public static async void HttpClientPost(string url) { var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.None }; using (var httpclient = new HttpClient(handler)) { httpclient.BaseAddress = new Uri(url); var content = new FormUrlEncodedContent(new Dictionary<string, string>() { {"name", "杜宗英"}, {"age", "18"}, {"gender","false" } }); var response = await httpclient.PostAsync(url, content); string responseString = await response.Content.ReadAsStringAsync(); MessageBox.Show(responseString); } } private void button5_Click(object sender, EventArgs e) { string url = this.txt_post_url.Text; HttpClientPost(url); } #endregion
6,WebClient方式 Post請求
#region WebClient方式 Post請求 /// <summary> /// /// </summary> /// <param name="url"></param> /// <param name="body">傳遞的引數</param> /// <returns></returns> public static string WebClientPost(string url,string body) { byte[] postData = Encoding.UTF8.GetBytes(body);//編碼,尤其是漢字,事先要看下抓取網頁的編碼方式 WebClient webClient=new WebClient(); webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");//採取POST方式必須加的header,如果改為GET方式的話就去掉這句話即可 byte[] responseData = webClient.UploadData(url, "POST", postData);//得到返回字元流 string srcString = Encoding.UTF8.GetString(responseData);//解碼 return srcString; } private void btn_WebClient_Post_Click(object sender, EventArgs e) { string url = this.txt_post_url.Text; string body = this.txt_post_parameter.Text; string srcString = WebClientPost(url, body); this.txt_WebClient_Post.Text += srcString; } #endregion