码迷,mamicode.com
首页 > Windows程序 > 详细

C#访问https地址实例

时间:2019-09-19 21:59:00      阅读:144      评论:0      收藏:0      [点我收藏+]

标签: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");
            IDictionary<string, string> parameters = new Dictionary<string, 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, IDictionary<string, 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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!