• C#访问https地址实例


    程序访问的网址从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();
            }
        }
    }
  • 相关阅读:
    JQuery高级
    Filter&Listener
    关于用户信息的一个综合案例
    JSP,EL和JSTL
    Cookie和Session
    Request和Response
    Servlet和HTTP请求协议
    Tomcat
    xml
    数组模拟堆
  • 原文地址:https://www.cnblogs.com/zhaoshizi/p/11552390.html
Copyright © 2020-2023  润新知