zoukankan      html  css  js  c++  java
  • 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;
      }
  • 相关阅读:
    Apache 性能配置优化
    大数据企业备份
    Docker限制容器可用的CPU
    docker运行容器后agetty进程cpu占用率100%
    Ubuntu下crontab命令的用法
    set -x与set +x指令
    CentOS设置程序开机自启动的方法
    maven 打JAR包资源文件指定路径与文件读取
    hadoop-maven项目打包成可执行的jar
    如何将maven项目打包成可执行的jar
  • 原文地址:https://www.cnblogs.com/FGang/p/11140808.html
Copyright © 2011-2022 走看看