相关博文:https://www.cnblogs.com/xu-yi/p/10061342.html
相关博文:https://www.cnblogs.com/zoujinhua/p/11313396.html
HttpWebRequest类:提供支持的属性和方法中定义WebRequst以及其他属性和方法,使用户直接通过 HTTP 与服务器交互。
HttpWebResponse类:用于生成 HTTP 独立客户端应用程序发送 HTTP 请求和接收 HTTP 响应。
public static string GetRequest(string serviceAddress, string strContent) { try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress); request.Method = "POST"; request.ContentType = "application/json"; using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream())) { dataStream.Write(strContent); dataStream.Close(); } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); string encoding = response.ContentEncoding; if (encoding == null || encoding.Length < 1) { encoding = "UTF-8"; //默认编码 } StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding)); string retString = reader.ReadToEnd(); return retString; } catch (Exception e) { return "error,请求外API服务时发生异常,错误描述:" + e.Message; } }