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

    xiaoxiao2021-04-12  49

    /// <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; }
    转载请注明原文地址: https://ju.6miu.com/read-667444.html

    最新回复(0)