zoukankan      html  css  js  c++  java
  • C#如何HttpWebRequest模拟登陆,获取服务端返回Cookie以便登录请求后使用

            public static string GetCookie(string requestUrlString, Encoding encoding, ref CookieContainer cookie)
            {
                //向服务端请求
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(requestUrlString);
                myRequest.ContentType = "application/x-www-form-urlencoded";
                myRequest.CookieContainer = new CookieContainer();
                //将请求的结果发送给客户端(界面、应用)
                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
                cookie.Add(myResponse.Cookies);
                StreamReader reader = new StreamReader(myResponse.GetResponseStream(), encoding);
                return reader.ReadToEnd();
            }
    
            public static string GetHtml(string requestUrlString, Encoding encoding, CookieContainer cookie)
            {
                string ua = "Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(requestUrlString);
                myRequest.ContentType = "application/x-www-form-urlencoded";
                myRequest.UserAgent = ua;
                myRequest.CookieContainer = cookie;
                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
                StreamReader reader = new StreamReader(myResponse.GetResponseStream(), encoding);
                return reader.ReadToEnd();
            }
    
            public static string PostLogin(string postData, string requestUrlString, ref CookieContainer cookie)
            {
                ASCIIEncoding encoding = new ASCIIEncoding();
                byte[] data = encoding.GetBytes(postData);
                //向服务端请求
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(requestUrlString);
                myRequest.Method = "POST";
                myRequest.ContentType = "application/x-www-form-urlencoded";
                myRequest.ContentLength = data.Length;
                myRequest.CookieContainer = new CookieContainer();
                Stream newStream = myRequest.GetRequestStream();
                newStream.Write(data, 0, data.Length);
                newStream.Close();
                //将请求的结果发送给客户端(界面、应用)
                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
                cookie.Add(myResponse.Cookies);
                StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
                return reader.ReadToEnd();
            }
    
            public static string PostRequest(string postData, string requestUrlString, CookieContainer cookie)
            {
                ASCIIEncoding encoding = new ASCIIEncoding();
                byte[] data = encoding.GetBytes(postData);
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(requestUrlString);
                myRequest.Method = "POST";
                myRequest.ContentType = "application/x-www-form-urlencoded";
                myRequest.ContentLength = data.Length;
                myRequest.CookieContainer = cookie;
                Stream newStream = myRequest.GetRequestStream();
                newStream.Write(data, 0, data.Length);
                newStream.Close();
                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
                StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
                return reader.ReadToEnd();
            }
  • 相关阅读:
    【MPI学习2】MPI并行程序设计模式:对等模式 & 主从模式
    【MPI学习1】简单MPI程序示例
    【多线程】零碎记录1
    【APUE】Chapter17 Advanced IPC & sign extension & 结构体内存对齐
    php-7.1编译记录
    rsyslogd系统日志服务总结
    php配置(php7.3)
    php-fpm回顾和总结
    php-fpm配置项
    Hive之执行计划分析(explain)
  • 原文地址:https://www.cnblogs.com/huangcong/p/7514327.html
Copyright © 2011-2022 走看看