C#访问https地址实例

2021-02-02 12:14

阅读:649

标签:webkit   bool   打印   ogr   log   login   manage   windows   line   

程序访问的网址从http换成了https,安全性更高了,程序对网址的访问也要改一下

C#访问https地址实例如下

namespace ConsoleApp2
{
    public class Program
    {
        protected void LoginTest()
        {
            string url = "https://passport.test.com/passport/login";
            Encoding encoding = Encoding.GetEncoding("utf-8");
            IDictionarystring, string> parameters = new Dictionarystring, string>();
            parameters.Add("username", "aaaaaa");
            parameters.Add("rsapwd", "111111");
            HttpWebResponse response = PostHttps(url, parameters, encoding);
            //打印返回值
            Stream stream = response.GetResponseStream();   //获取响应的字符串流
            StreamReader sr = new StreamReader(stream); //创建一个stream读取流
            string html = sr.ReadToEnd();   //从头读到尾,放到字符串html
            Console.WriteLine(html);
        }

        public static void Main()
        {
            Program pro = new Program();
            pro.LoginTest();
        }
       
        private static readonly string DefaultUserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36";

        private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            return true; //总是接受   
        }

        public static HttpWebResponse PostHttps(string url, IDictionarystring, string> parameters, Encoding charset)
        {
            HttpWebRequest request = null;
            CookieContainer cookie = new CookieContainer();
            //HTTPSQ请求
            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
            request = WebRequest.Create(url) as HttpWebRequest;
            request.CookieContainer = cookie;
            request.ProtocolVersion = HttpVersion.Version11;
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.UserAgent = DefaultUserAgent;
            request.KeepAlive = true;
            request.Headers["Accept-Language"] = "zh-CN,zh;q=0.9";
            //request.Headers["Cookie"] = "username=aaaaaa; Language=zh_CN";
            //如果需要POST数据   
            if (!(parameters == null || parameters.Count == 0))
            {
                StringBuilder buffer = new StringBuilder();
                int i = 0;
                foreach (string key in parameters.Keys)
                {
                    if (i > 0)
                    {
                        buffer.AppendFormat("&{0}={1}", key, WebUtility.UrlEncode(parameters[key]));
                    }
                    else
                    {
                        buffer.AppendFormat("{0}={1}", key, WebUtility.UrlEncode(parameters[key]));
                    }
                    i++;
                }
                byte[] data = charset.GetBytes(buffer.ToString());
                using (Stream stream = request.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                }
            }

            return (HttpWebResponse)request.GetResponse();
        }
    }
}

 

C#访问https地址实例

标签:webkit   bool   打印   ogr   log   login   manage   windows   line   

原文地址:https://www.cnblogs.com/zhaoshizi/p/11552390.html


评论


亲,登录后才可以留言!