• C# 后台服务器端 Get 请求函数封装


    1. 请求参数封装
      /// <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));
      }
    2. 请求方式函数封装
      /// <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;
      }
  • 相关阅读:
    02.ZooKeeper的Java客户端使用
    01.ZooKeeper安装和介绍
    02.Elasticsearch入门
    01.Elasticsearch安装
    01.ActiveMQ安装部署
    springboot项目打包时提示“程序包xxx不存在,找不到符号”
    Eclipse提交git代码 报错authentication not supported
    Eclipse提交git代码 报错authentication not supported
    utf8mb4_general_ci报错解决方案
    mysql开启远程访问
  • 原文地址:https://www.cnblogs.com/FGang/p/11140808.html
Copyright © 2020-2023  润新知