private AopDictionary buildRequestParams <T>(IAopRequest <T> request, String accessToken, String appAuthToken) where T : AopResponse { // 默认参数 AopDictionary oriParams = new AopDictionary(request.GetParameters()); // 序列化BizModel AopDictionary result = SerializeBizModel(oriParams, request); // 获取参数 String charset = String.IsNullOrEmpty(this.charset) ? "utf-8" : this.charset; String apiVersion = String.IsNullOrEmpty(request.GetApiVersion()) ? this.Version : request.GetApiVersion(); // 添加协议级请求参数,为空的参数后面会自动过滤,这里不做处理。 result.Add(METHOD, request.GetApiName()); result.Add(VERSION, apiVersion); result.Add(APP_ID, appId); result.Add(FORMAT, format); result.Add(TIMESTAMP, DateTime.Now); result.Add(ACCESS_TOKEN, accessToken); result.Add(SIGN_TYPE, signType); result.Add(TERMINAL_TYPE, request.GetTerminalType()); result.Add(TERMINAL_INFO, request.GetTerminalInfo()); result.Add(PROD_CODE, request.GetProdCode()); result.Add(NOTIFY_URL, request.GetNotifyUrl()); result.Add(CHARSET, charset); result.Add(RETURN_URL, request.GetReturnUrl()); result.Add(APP_AUTH_TOKEN, appAuthToken); if (request.GetNeedEncrypt()) { if (String.IsNullOrEmpty(result[BIZ_CONTENT])) { throw new AopException("api request Fail ! The reason: encrypt request is not supported!"); } if (String.IsNullOrEmpty(this.encyptKey) || String.IsNullOrEmpty(this.encyptType)) { throw new AopException("encryptType or encryptKey must not null!"); } if (!"AES".Equals(this.encyptType)) { throw new AopException("api only support Aes!"); } String encryptContent = AopUtils.AesEncrypt(this.encyptKey, result[BIZ_CONTENT], this.charset); result.Remove(BIZ_CONTENT); result.Add(BIZ_CONTENT, encryptContent); result.Add(ENCRYPT_TYPE, this.encyptType); } return(result); }
public T Execute <T>(IAopRequest <T> request, string accessToken, string appAuthToken) where T : AopResponse { if (string.IsNullOrEmpty(this.charset)) { this.charset = "utf-8"; } string apiVersion = null; if (!string.IsNullOrEmpty(request.GetApiVersion())) { apiVersion = request.GetApiVersion(); } else { apiVersion = Version; } // 添加协议级请求参数 AopDictionary txtParams = new AopDictionary(request.GetParameters()); // 序列化BizModel txtParams = SerializeBizModel(txtParams, request); txtParams.Add(METHOD, request.GetApiName()); txtParams.Add(VERSION, apiVersion); txtParams.Add(APP_ID, appId); txtParams.Add(FORMAT, format); txtParams.Add(TIMESTAMP, DateTime.Now); txtParams.Add(ACCESS_TOKEN, accessToken); txtParams.Add(SIGN_TYPE, signType); txtParams.Add(TERMINAL_TYPE, request.GetTerminalType()); txtParams.Add(TERMINAL_INFO, request.GetTerminalInfo()); txtParams.Add(PROD_CODE, request.GetProdCode()); txtParams.Add(CHARSET, charset); if (!string.IsNullOrEmpty(request.GetNotifyUrl())) { txtParams.Add(NOTIFY_URL, request.GetNotifyUrl()); } if (!string.IsNullOrEmpty(appAuthToken)) { txtParams.Add(APP_AUTH_TOKEN, appAuthToken); } if (request.GetNeedEncrypt()) { if (string.IsNullOrEmpty(txtParams[BIZ_CONTENT])) { throw new AopException("api request Fail ! The reason: encrypt request is not supported!"); } if (string.IsNullOrEmpty(this.encyptKey) || string.IsNullOrEmpty(this.encyptType)) { throw new AopException("encryptType or encryptKey must not null!"); } if (!"AES".Equals(this.encyptType)) { throw new AopException("api only support Aes!"); } string encryptContent = AopUtils.AesEncrypt(this.encyptKey, txtParams[BIZ_CONTENT], this.charset); txtParams.Remove(BIZ_CONTENT); txtParams.Add(BIZ_CONTENT, encryptContent); txtParams.Add(ENCRYPT_TYPE, this.encyptType); } // 添加签名参数 txtParams.Add(SIGN, AopUtils.SignAopRequest(txtParams, privateKeyPem, charset, this.keyFromFile, signType)); // 是否需要上传文件 string body; if (request is IAopUploadRequest <T> ) { IAopUploadRequest <T> uRequest = (IAopUploadRequest <T>)request; IDictionary <string, FileItem> fileParams = AopUtils.CleanupDictionary(uRequest.GetFileParameters()); body = webUtils.DoPost(this.serverUrl + "?" + CHARSET + "=" + this.charset, txtParams, fileParams, this.charset); } else { body = webUtils.DoPost(this.serverUrl + "?" + CHARSET + "=" + this.charset, txtParams, this.charset); } T rsp = null; IAopParser <T> parser = null; if ("xml".Equals(format)) { parser = new AopXmlParser <T>(); rsp = parser.Parse(body, charset); } else { parser = new AopJsonParser <T>(); rsp = parser.Parse(body, charset); } ResponseParseItem item = parseRespItem(request, body, parser, this.encyptKey, this.encyptType, charset); rsp = parser.Parse(item.realContent, charset); CheckResponseSign(request, item.respContent, rsp.IsError, parser, this.alipayPublicKey, this.charset, signType, this.keyFromFile); return(rsp); }