WebApi(二)
程序开发
2023-09-17 22:01:48
1.接口控制器使用
2.Postman测试
3.C#调运测试
1.接口控制器调用
生成,并启用
2.Postman测试
3.C#端调用
private void button1_Click(object sender, EventArgs e){try{int a = int.Parse(textBox1.Text);int b = int.Parse(textBox2.Text);string ss = "0";switch (comboBox1.Text){case "GET":ss = HttpGet(txtURL.Text + "?a=" + a + "&" + "b=" + b);break;case "POST":ss = HttpPost(txtURL.Text + "?a=" + a + "&" + "b=" + b, "");break;case "PUT":ss = HttpPut(txtURL.Text + "?a=" + a + "&" + "b=" + b, "");break;case "DELETE":ss = HttpDelete(txtURL.Text + "?a=" + a + "&" + "b=" + b, "");break;default:break;}MessageBox.Show(ss);}catch (Exception ex){MessageBox.Show(ex.Message);}}#region WebRequest方式public static string HttpPost(string url, string body){//ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);Encoding encoding = Encoding.UTF8;HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);request.Method = "POST";request.Accept = "text/html, application/xhtml+xml, */*";request.ContentType = "application/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();}}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();}}public static string HttpPut(string url, string json){Encoding encoding = Encoding.UTF8;HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);request.Method = "PUT";request.Accept = "text/html, application/xhtml+xml, */*";request.ContentType = "application/json";byte[] buffer = encoding.GetBytes(json);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();}}public static string HttpDelete(string url, string json){Encoding encoding = Encoding.UTF8;HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);request.Method = "DELETE";request.Accept = "text/html, application/xhtml+xml, */*";request.ContentType = "application/json";byte[] buffer = encoding.GetBytes(json);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();}}#endregion
标签:
上一篇:
Android 底部导航栏(二、自定义View Fragment)
下一篇:
相关文章
-
无相关信息