当前位置:首页 > Web开发 > 正文

appId); dic.Add("timestamp"

2024-03-31 Web开发

/// <summary> /// Http 请求 通用来 /// </summary> public abstract class HttpRequest { public static string HttpAccept_JSON_UTF8 = "application/json;charset=UTF-8"; public static string HttpAccept_XML = "application/xml;"; public static int? reqest_timeout = 15000; /// <summary> /// 倡议 HttpGet 请求 /// </summary> /// <param></param> /// <param></param> /// <param>请求超时前期待的毫秒数。默认值是 15,000 毫秒(15 秒)。</param> /// <param>application/xml、application/json、application/text、application/x-www-form-urlencoded</param> /// <param>接受的数据类型</param> /// <returns></returns> public static string HttpGet(string requestUriString,string content=null,int timeout= 0, string contentType =null, string httpAccept=null)//get方法提交数据 { //计数器用于识别请求 long num = DateTime.Now.Ticks; contentType = contentType ?? "application/x-www-form-urlencoded"; httpAccept = httpAccept ?? HttpAccept_XML; string url = requestUriString; if (url.IndexOf("?") <0) { url += "?"; } url = url + content; HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); request.Method = "GET"; request.ContentType = contentType; request.Accept = httpAccept ?? HttpAccept_XML; request.Timeout = timeout > 0 ? timeout : reqest_timeout.Value; request.AllowAutoRedirect = false; WebResponse response = null; string responseStr = null; try { response = request.GetResponse(); if (response != null) { StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); responseStr = reader.ReadToEnd(); reader.Close(); } } catch (Exception ex) { //LogService.Error($"HttpGet 请求堕落 url:{url}", ex); } finally { request = null; response = null; } return responseStr; } /// <summary> /// HTTP POST方法请求数据 /// </summary> /// <param>URL</param> /// <param>POST的数据</param> /// <param>application/xml、application/json、application/text、application/x-www-form-urlencoded</param> /// <param>接受的数据类型</param> /// <param>超不时间毫秒</param> /// <returns>请求返回数据</returns> public static string HttpPost(string url, string content, int? timeout = null, string contentType = null, string httpAccept =null) { //计数器用于识别请求 long num = DateTime.Now.Ticks; contentType = contentType ?? "application/x-www-form-urlencoded"; httpAccept = httpAccept ?? HttpAccept_XML; timeout = timeout?? reqest_timeout; HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); request.Method = "POST"; request.ContentType = contentType; request.Accept = httpAccept ; request.Timeout = timeout.Value; request.AllowAutoRedirect = false; StreamWriter requestStream = null; WebResponse response = null; string responseStr = null; try { requestStream = new StreamWriter(request.GetRequestStream()); requestStream.Write(content); requestStream.Close(); response = request.GetResponse(); if (response != null) { StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); responseStr = reader.ReadToEnd(); reader.Close(); } } catch (Exception ex) { //LogService.Error($"HttpPost 请求堕落 URL:{url} Content:{content}", ex); } finally { request = null; requestStream = null; response = null; } return responseStr; } }


要领挪用:
string url="";//接口地点 string content="";//参数

var dic = new Dictionary<string, string>();
  dic.Add("appid", appId);
  dic.Add("timestamp", timeStamp);
  dic.Add("sign", sign);

string jsonResult = HttpRequest.HttpPost(url, content, 15000, "application/json", "application/json", dic);

string jsonResult = HttpRequest.HttpGet(url, content, 0, "application/json", null, dic);

HttpWebRequest挪用接口

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/31685.html