- 请求参数封装
/// <summary> /// 拼接 Get请求参数 /// </summary> /// <param name="parames"></param> /// <returns></returns> private static Tuple<string, string> GetQueryString(Dictionary<string, string> parames) { StringBuilder query = new StringBuilder(""); StringBuilder queryStr = new StringBuilder(""); if (parames == null || parames.Count == 0) return new Tuple<string, string>("", ""); foreach (var item in parames) { string key = item.Key; string value = item.Value; if (!string.IsNullOrEmpty(key)) { query.Append(key).Append(value); queryStr.Append("&").Append(key).Append("=").Append(value); } } return new Tuple<string, string>(query.ToString(), queryStr.ToString().Substring(1, queryStr.Length - 1)); }
- 请求方式函数封装
/// <summary> /// 请求连接 函数封装 /// </summary> /// <param name="requestType">请求方式:Get、Post等</param> /// <param name="requestLink">连接地址</param> /// <param name="query">请求参数</param> /// <returns></returns> public static string GetWeChatToken(string requestType,string requestLink, string query) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestLink + "?" + query); request.Method = requestType; request.ContentType = "application/json"; request.Timeout = 90000; request.Headers.Set("Pragma", "no-cache"); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream streamReceive = response.GetResponseStream(); StreamReader streamReader = new StreamReader(streamReceive, Encoding.UTF8); result = streamReader.ReadToEnd(); streamReader.Close(); streamReceive.Close(); request.Abort(); response.Close(); return result; }
相关文章
- 10-02C# 动态创建SQL数据库(二) 在.net core web项目中生成二维码 后台Post/Get 请求接口 方式 WebForm 页面ajax 请求后台页面 方法 实现输入框小数多 自动进位展示,编辑时实际值不变 快速掌握Gif动态图实现代码 C#处理和对接HTTP接口请求
- 10-02vue-cli配置axios,并基于axios进行后台请求函数封装
- 10-02C# 后台服务器端 Get 请求函数封装
- 10-02C# http请求相关的函数 HttpWebRequest: Post , Get ; PostAndRedirect
- 10-02C# 中经常用到的HTTP请求类,已封装get,post,delete,put,Patch
- 10-02vue-cli配置axios,并基于axios进行后台请求函数封装