HttpHelper 逃离我推掉我的手 2023-10-18 22:59 86阅读 0赞 using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Net.Http.Headers; using System.Web; namespace YFAPICommon.Libs { public class HttpHelper { static public string serverUrl = "http://192.168.0.86:8080/"; public static JObject Post(string url, Dictionary<string, object> param) { using (HttpClient client = new HttpClient()) { client.BaseAddress = new Uri(serverUrl); //OAuth2令牌 client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "xxxx"); //请求超时 client.Timeout = new TimeSpan(5000); var httpContent = new StringContent(JsonConvert.SerializeObject(param)); httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); var response = client.PostAsync(url, httpContent).Result; var responseValue = response.Content.ReadAsStringAsync().Result; if (response.StatusCode == System.Net.HttpStatusCode.OK) { return JObject.Parse(responseValue); } return null; } } public static JObject Get(string url) { using (HttpClient client = new HttpClient()) { client.BaseAddress = new Uri(serverUrl); //OAuth2令牌 client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "xxxx"); //请求超时 client.Timeout = new TimeSpan(5000); var response = client.GetAsync(url).Result; var responseValue = response.Content.ReadAsStringAsync().Result; if (response.StatusCode == System.Net.HttpStatusCode.OK) { return JObject.Parse(responseValue); } return null; } } } } using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System.Collections.Generic; using System.IO; using System.Net; namespace YFAPICommon.Controllers { public class HttpHelper { public static JObject Post(string url, Dictionary<string, object> param) { string paramStr = JsonConvert.SerializeObject(param); byte[] data = System.Text.Encoding.UTF8.GetBytes(paramStr); var request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/json"; Stream newStream = request.GetRequestStream(); newStream.Write(data, 0, data.Length); newStream.Close(); request.ServicePoint.Expect100Continue = false; request.ProtocolVersion = HttpVersion.Version11; var response = (HttpWebResponse)request.GetResponse(); var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd(); if (response.StatusCode == System.Net.HttpStatusCode.OK) { return JObject.Parse(responseString); } return null; } public static JObject Get(string url) { var request = (HttpWebRequest)WebRequest.Create(url); var response = (HttpWebResponse)request.GetResponse(); var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd(); if (response.StatusCode == System.Net.HttpStatusCode.OK) { return JObject.Parse(responseString); } return null; } } }
相关 HttpHelper using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using S 逃离我推掉我的手/ 2023年10月18日 22:59/ 0 赞/ 87 阅读
相关 php httphelper,helper.php-1 //------------------------ // ThinkPHP 助手函数 //------------------------- use think\\Ca 系统管理员/ 2022年11月17日 13:57/ 0 赞/ 99 阅读
相关 c# HttpHelper类 Http请求通用方法 下面是HttpHelper类 using System; using System.Collections.Generic; using Syst 骑猪看日落/ 2022年04月16日 00:21/ 0 赞/ 222 阅读
还没有评论,来说两句吧...