• 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;
      }
  • 相关阅读:
    Swift学习-Property
    Swift学习-protocol
    Swift学习-Class
    Swift学习-Enumerate、Structure
    iOS“此时无法下载应用”解决办法
    iTunes Connect用户职能与权限
    TestFlight Beta Testing 开发指南中英对照
    梳理一下KVC
    mac下使用github 上传代码(转)
    运用Runtime全局修改UILabel的默认字体
  • 原文地址:https://www.cnblogs.com/FGang/p/11140808.html
Copyright © 2020-2023  润新知