类别:程序开发

日期:2024-07-10 浏览:918 评论: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
作者授权:除特别说明外,本文由 零一 原创编译并授权 零一的世界 刊载发布。
版权声明:本文不使用任何协议授权,您可以任何形式自由转载或使用。
 您阅读本篇文章共花了: 

历史上的今天
07月
10

 可能感兴趣的文章

评论区

发表评论 / 取消回复

必填

选填

选填

◎欢迎讨论,请在这里发表您的看法及观点。