类别:程序开发
日期:2024-07-10 浏览:1420 评论:0
接口调用方法:
public class HttpsHelper { private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; //总是接受 } public static string Get(string Url) { try { HttpWebRequest request = null; request = WebRequest.Create(Url) as HttpWebRequest; ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); request.ProtocolVersion = HttpVersion.Version11; request.KeepAlive = false; // 这里设置了协议类型。 ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; ServicePointManager.DefaultConnectionLimit = 100; ServicePointManager.Expect100Continue = true; request.Method = "GET"; //使用get方式发送数据 request.ContentType = "application/json"; request.AllowAutoRedirect = true; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream stream = response.GetResponseStream(); string result = string.Empty; using (StreamReader sr = new StreamReader(stream)) { result = sr.ReadToEnd(); } return result; } catch (Exception ex) { return ex.Message; } } public static string Post(string url, Dictionary<string, string> pairs) { try { HttpWebRequest request = null; request = WebRequest.Create(url) as HttpWebRequest; ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); request.ProtocolVersion = HttpVersion.Version11; // 这里设置了协议类型。 ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; ServicePointManager.DefaultConnectionLimit = 100; ServicePointManager.Expect100Continue = true; request.Method = "POST"; request.ContentType = "application/json"; request.AllowAutoRedirect = true; string content = JsonConvert.SerializeObject(pairs); byte[] formData = Encoding.UTF8.GetBytes(content); request.ContentLength = formData.Length; Stream newStream = request.GetRequestStream(); newStream.Write(formData, 0, formData.Length); newStream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream stream = response.GetResponseStream(); string result = string.Empty; using (StreamReader sr = new StreamReader(stream)) { result = sr.ReadToEnd(); } return result; } catch (Exception ex) { return ex.Message; } } public static string PostToken(string url, Dictionary<string, string> pairs) { try { HttpWebRequest request = null; request = WebRequest.Create(url) as HttpWebRequest; ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); request.ProtocolVersion = HttpVersion.Version11; // 这里设置了协议类型。 ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; ServicePointManager.DefaultConnectionLimit = 100; ServicePointManager.Expect100Continue = true; request.Method = "POST"; request.ContentType = "application/json"; request.AllowAutoRedirect = true; request.Headers.Add("Authorization", ""); string content = JsonConvert.SerializeObject(pairs); byte[] formData = Encoding.UTF8.GetBytes(content); request.ContentLength = formData.Length; Stream newStream = request.GetRequestStream(); newStream.Write(formData, 0, formData.Length); newStream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream stream = response.GetResponseStream(); string result = string.Empty; using (StreamReader sr = new StreamReader(stream)) { result = sr.ReadToEnd(); } return result; } catch (Exception ex) { return ex.Message; } } }
本文标题:C#对接Https接口
本文链接:https://vtzw.com/post/1133.html
版权声明:本文不使用任何协议授权,您可以任何形式自由转载或使用。
发表评论 / 取消回复