using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; namespace iWareCommon.Utils { public static class HttpHelper { /// /// Post /// /// 请求后台地址 /// json格式参数 /// public static string Post(string url, string json) { string result = ""; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Method = "POST"; req.ContentType = "application/json"; var authorization = "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VySWQiOjE0MjMwNzA3MDkxMDU1MSwiVGVuYW50SWQiOjE0MjMwNzA3MDkxODc4MCwiQWNjb3VudCI6InN1cGVyQWRtaW4iLCJOYW1lIjoi6LaF57qn566h55CG5ZGYIiwiU3VwZXJBZG1pbiI6MSwiT3JnSWQiOiIwIiwiT3JnTmFtZSI6bnVsbCwiaWF0IjoxNjYwMjY4MzA5LCJuYmYiOjE2NjAyNjgzMDksImV4cCI6MTY2MDMyODI0OSwiaXNzIjoiZGlsb24iLCJhdWQiOiJkaWxvbiJ9.lKxygZjORQhQQbbWYOcHtMCXKTEueQMr7Fi1sWjp_1A"; req.Headers.Add("Authorization", authorization); #region 添加Post 参数 if (!string.IsNullOrWhiteSpace(json)) { byte[] data = Encoding.UTF8.GetBytes(json.ToString()); req.ContentLength = data.Length; using (Stream reqStream = req.GetRequestStream()) { reqStream.Write(data, 0, data.Length); reqStream.Close(); } } #endregion HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); Stream stream = resp.GetResponseStream(); //获取响应内容 using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { result = reader.ReadToEnd(); } return result; } } }