private TResponse InvokeApiAsync <TRequest, TResponse>(string url, TRequest request) where TRequest : OBaseRequest where TResponse : OBaseResponse { using (HttpClient client = new HttpClient()) { request.Sign = WeChatSignHelper.CreateSign <TRequest>(this.m_ApiKey, request); StringContent content = new StringContent(XmlSerializerHelper.GetXmlString <TRequest>(request)); TResponse response = XmlSerializerHelper.FromXmlString <TResponse>(client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result); if (!response.IsSuccess) { response.ReturnMsg = this.GetErrorMessage(response, url); } else if (!this.ValidateResponse <TResponse>(response)) { response.ResultCode = string.Empty; response.ReturnMsg = "返回结果响应结果不正确"; } return(response); } }
private TResponse InvokeApiWithCertificateAsync <TRequest, TResponse>(string url, TRequest request) where TRequest : OBaseRequest where TResponse : OBaseResponse { X509Certificate2 certificate = new X509Certificate2(this.m_CertificatePath, this.m_CertificatePassword, X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet); var handler = new HttpClientHandler { ClientCertificateOptions = ClientCertificateOption.Manual, SslProtocols = SslProtocols.Tls12, ServerCertificateCustomValidationCallback = (x, y, z, m) => true, }; handler.ClientCertificates.Add(certificate); using (HttpClient client = new HttpClient(handler)) { request.Sign = WeChatSignHelper.CreateSign <TRequest>(this.m_ApiKey, request); StringContent content = new StringContent(XmlSerializerHelper.GetXmlString <TRequest>(request)); TResponse response = XmlSerializerHelper.FromXmlString <TResponse>(client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result); if (!response.IsSuccess) { response.ReturnMsg = this.GetErrorMessage(response, url); } return(response); } }
private bool ValidateResponse <T>(T response) where T : OBaseResponse { string str = WeChatSignHelper.CreateSign <T>(this.m_ApiKey, response); return(response.Sign.Equals(str, StringComparison.OrdinalIgnoreCase)); }