- 请求参数封装
/// <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; }