对接口参数进行加密调用接口获取信息

本文介绍如何通过对接口参数进行加密,如哈希算法MD5和HS256,来安全地调用接口并获取所需信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        /// <summary>
        /// 调用接口获取信息
        /// </summary>
        /// <param name="p_strID"></param>
        /// <returns></returns>
        public Dictionary<string, object> GetInformationService(string p_strID)
        {
            const string strUrl = "http://www.baidu.com/api/test.php?query=test&moudle=moudleName";
            WebServiceMethod webServiceMethod = new WebServiceMethod(strUrl, "");
            Dictionary<string, string> dataDic = new Dictionary<string, string>();
            dataDic.Add("id", p_strID);
            var dataJson = (new JavaScriptSerializer()).Serialize(dataDic);
            string secretSign = WebServiceMethod.ConvertJsonToSecretSign(dataJson);
            string queryString = "data=" + dataJson + "&secSign=" + secretSign;
            var response = webServiceMethod.PostRequest(queryString);
            Dictionary<string, object> jsonDesToDic = webServiceMethod.ConvertValueAddResponseToObject(response);
            return jsonDesToDic;
        }

WebServiceMethod类

 //加密方式
......
//查询
public HttpWebResponse PostRequest(string strRequestParams)
   {
    //建立HttpWebRequest
       HttpWebRequest req = (HttpWebRequest)WebRequest.Create(this.WebRequestURL); 
       //定义网关
       if (Proxy != "")
       {
           WebProxy objProxy = new WebProxy(this.Proxy);   
           req.Proxy = objProxy;
       }
       req.Accept = "application/xml";
       req.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
       req.KeepAlive = false;
       req.Method = "POST";
       if (req.Method == "POST")
       {
           byte[] b = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(strRequestParams);
           req.ContentLength = b.Length;
           try
           {
               Stream oSRe = req.GetRequestStream();
               //添加接口参数到流
               oSRe.Write(b, 0, b.Length);
               oSRe.Close();
               oSRe = null;
           }
           catch (Exception)
           {
               req = null;
               return null;
           }

       }
       HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse();
       return myResponse;
   }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值