使用HttpClient和HttpWebRequest调用WebAPI接口
这里使用ASP.NET Core创建一个WebApi项目,代码如下:
using System.IO; using Microsoft.AspNetCore.Mvc; namespace AspNetCoreDemo.Controllers { [Route("api/TestHttp")] [ApiController] public class TestHttpController : ControllerBase { [HttpGet] public ActionResult<string> Get() { return "这是无参的Get方法"; } [HttpGet("{name}")] public ActionResult<string> Get(string name) { return $"这是有参的Get方法,参数{name}"; } [HttpPost] public void Post([FromBody] string para) { // 模拟记录日志 using (StreamWriter sw = new StreamWriter(@"E:\test.txt", true)) { sw.WriteLine(para); sw.Close(); } } } }
然后分别使用HttpClient和HttpWebRequest调用提供的WebApi接口,代码如下:
using Newtonsoft.Json; using System; using System.IO; using System.Net; using System.Net.Http; using System.Text; namespace ConsoleApp1 { class Program { static void Main(string[] args) { string result1 = HttpGet(); string result2 = HttpGetWithPara("有参方法"); string result3 = HttpPost("post"); PostClient(); GetClient(); GetParaClient("2333"); Console.WriteLine(result1); Console.WriteLine(result2); Console.ReadKey(); } #region HttpWebRequest调用API接口 /// <summary> /// 通过HttpWebRequest调用无参的Get方法 /// </summary> static string HttpGet() { string retStr = ""; HttpWebRequest request = null; try { string fullUrl = "http://localhost:38149/api/TestHttp"; request = (HttpWebRequest)HttpWebRequest.Create(fullUrl); request.Method = "GET"; request.ContentType = "application/x-www-form-urlencoded"; request.Credentials = CredentialCache.DefaultCredentials; request.Timeout = 3600; request.ReadWriteTimeout = 3600; retStr = ReadStringResponse(request.GetResponse()); } catch (Exception ex) { throw ex; } return retStr; } /// <summary> /// 通过HttpWebRequest调用有参数的Get方法 /// </summary> /// <param name="para"></param> /// <returns></returns> static string HttpGetWithPara(string para) { string retStr = ""; HttpWebRequest request = null; try { string fullUrl = $"http://localhost:38149/api/TestHttp/{para}"; request = (HttpWebRequest)HttpWebRequest.Create(fullUrl); request.Method = "GET"; request.ContentType = "application/x-www-form-urlencoded"; request.Credentials = CredentialCache.DefaultCredentials; request.Timeout = 3600; request.ReadWriteTimeout = 3600; retStr = ReadStringResponse(request.GetResponse()); } catch (Exception ex) { throw ex; } return retStr; } /// <summary> /// 解析web响应,以string形式返回 /// </summary> /// <param name="response">web响应</param> /// <returns>返回string形式的web响应</returns> private static String ReadStringResponse(WebResponse response) { string returnStr = ""; StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8); returnStr = sr.ReadToEnd(); sr.Close(); return returnStr; } /// <summary> /// 通过HttpWebRequest调用Post方法 /// </summary> /// <param name="body"></param> /// <returns></returns> static string HttpPost(string body) { string url = "http://localhost:38149/api/TestHttp"; Encoding encoding = Encoding.UTF8; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/json;charset=utf-8"; byte[] buffer = encoding.GetBytes(body); request.ContentLength = buffer.Length; Stream stream = request.GetRequestStream(); stream.Write(buffer, 0, buffer.Length); stream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding)) { return reader.ReadToEnd(); } } #endregion #region HttpClient调用API接口 /// <summary> /// 通过HttpClient调用Get方法 /// </summary> static async void GetClient() { string url = "http://localhost:38149/api/TestHttp"; try { var str = JsonConvert.SerializeObject("213123"); HttpContent content = new StringContent(str); content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); using (var client = new HttpClient()) { HttpResponseMessage response = await client.GetAsync(url);//改成自己的 response.EnsureSuccessStatusCode();//用来抛异常的 string responseBody = await response.Content.ReadAsStringAsync(); } } catch (Exception ex) { } } /// <summary> /// 通过HttpClient调用有参的Get方法 /// </summary> /// <param name="para"></param> static async void GetParaClient(string para) { string strReturn = ""; string url = $"http://localhost:38149/api/TestHttp/{para}"; try { var str = JsonConvert.SerializeObject("213123"); HttpContent content = new StringContent(str); content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); using (var client = new HttpClient()) { HttpResponseMessage response = await client.GetAsync(url); response.EnsureSuccessStatusCode();//用来抛异常的 strReturn = await response.Content.ReadAsStringAsync(); } } catch (Exception ex) { strReturn = ex.Message; } } /// <summary> /// 通过HttpClient调用Post方法 /// </summary> static async void PostClient() { string url = "http://localhost:38149/api/TestHttp"; try { var str = JsonConvert.SerializeObject("213123"); HttpContent content = new StringContent(str); content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); using (var client = new HttpClient()) { HttpResponseMessage response = await client.PostAsync(url, content);//改成自己的 response.EnsureSuccessStatusCode();//用来抛异常的 string responseBody = await response.Content.ReadAsStringAsync(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } #endregion } }