前言

过程,如图:

构建API

第一步创建一个帮助类,类里面提供了加密、组装Url等方法,代码如下:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Web;
  5 using System.IO;
  6 using System.Net;
  7 using System.Security.Cryptography;
  8 using System.Text; 
  9 
 10 namespace APIClient.Content
 11 {
 12     public class WebHelper
 13     {
 14 
 15         #region 获得客户端时间+ public static int GetTimeZ(DateTime time)
 16         public static int GetTimeZ(DateTime time)
 17         {
 18             var startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
 19             return (int)(time - startTime).TotalSeconds;
 20         }
 21         #endregion
 22 
 23         #region 加密key+private string CreateSign(Dictionary<string, string> paramList, string appKey)
 24         public static string CreateSign(Dictionary<string, string> paramList, string appKey)
 25         {
 26             var str = paramList.OrderBy(c => c.Key).Aggregate(string.Empty, (current, item) => current + item.Value);
 27             return MD5(str + appKey).ToLower();//调用MD5
 28         }
 29         #endregion
 30 
 31         #region 执行HTTP GET请求+ public static string DoGet(string url, IDictionary<string, string> parameters)
 32         public static string DoGet(string url, IDictionary<string, string> parameters)
 33         {
 34             if (parameters != null && parameters.Count > 0)
 35             {
 36                 if (url.Contains("?"))
 37                 {
 38                     url = url + "&" + BuildPostDate(parameters); //调用BuildPostDate
 39                 }
 40                 else
 41                 {
 42                     url = url + "?" + BuildPostDate(parameters);
 43                 }
 44             }
 45 
 46             HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
 47             req.Method = "GET";
 48             req.KeepAlive = true;
 49             req.UserAgent = "ADCSDK";
 50             req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
 51             HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
 52             Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);
 53             return GetResponseAsString(rsp, encoding); //调用GetResponseAsString
 54         }
 55 
 56         #endregion
 57 
 58         #region MD5加密+ public static string MD5(string input)
 59         /// <summary>
 60         /// MD5加密 (添加命名空间;using System.Security.Cryptography;)
 61         /// </summary>
 62         /// <param name="input">输入字符串</param>
 63         /// <returns>加密后的字符串</returns>
 64         public static string MD5(string input)
 65         {
 66             if (string.IsNullOrEmpty(input))
 67             {
 68                 return string.Empty;
 69             }
 70 
 71             var md5 = new MD5CryptoServiceProvider();
 72             var inputBytes = Encoding.UTF8.GetBytes(input);
 73             var outPutbytes = md5.ComputeHash(inputBytes);
 74             return BitConverter.ToString(outPutbytes).Replace("-", "");
 75         }
 76         #endregion
 77 
 78         #region 组装普通文本请求参数+ private static string BuildPostDate(IDictionary<string, string> parameters)
 79         private static string BuildPostDate(IDictionary<string, string> parameters)
 80         {
 81             StringBuilder postDate = new StringBuilder();
 82             bool hasParam = false;
 83             IEnumerator<KeyValuePair<string, string>> dem = parameters.GetEnumerator();
 84             while (dem.MoveNext())
 85             {
 86                 string name = dem.Current.Key;
 87                 string value = dem.Current.Value;
 88                 if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(value))
 89                 {
 90                     if (hasParam)
 91                     {
 92                         postDate.Append("&");
 93                     }
 94                     postDate.Append(name);
 95                     postDate.Append("=");
 96                     postDate.Append(Uri.EscapeDataString(value));
 97                     hasParam = true;
 98                 }
 99             }
100             return postDate.ToString();
101         }
102         #endregion
103 
104         #region 把响应流转换为文本+ private static string GetResponseAsString(HttpWebResponse rsp, Encoding encoding)
105         private static string GetResponseAsString(HttpWebResponse rsp, Encoding encoding)
106         {
107             StringBuilder result = new StringBuilder();
108             Stream stream = null;
109             StreamReader reader = null;
110 
111             try
112             {
113                 stream = rsp.GetResponseStream();
114                 reader = new StreamReader(stream, encoding);
115                 var buffer = new char[256];
116                 int readBytes;
117                 while ((readBytes = reader.Read(buffer, 0, buffer.Length)) > 0)
118                 {
119                     result.Append(buffer, 0, readBytes);
120                 }
121             }
122             finally
123             {
124                 if (reader != null) reader.Close();
125                 if (stream != null) stream.Close();
126                 if (rsp != null) rsp.Close();
127             }
128             return result.ToString();
129         }
130         #endregion
131 
132 
133     }
134 }
View Code

相关文章:

  • 2022-12-23
  • 2021-09-25
  • 2022-12-23
  • 2021-08-01
  • 2021-05-19
  • 2021-07-12
  • 2021-06-17
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-19
  • 2022-12-23
  • 2021-08-30
  • 2021-10-05
相关资源
相似解决方案