• HttpWebRequest模拟请求


    HttpWebRequest request;
    HttpWebResponse response;
    CookieCollection cookies = new CookieCollection();

    request = (HttpWebRequest)WebRequest.Create("http://47.97.109.186/cas/login");
    request.Method = "GET";
    request.Referer = "";
    request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3";
    request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36";
    request.ContentType = "text/html;charset=UTF-8";
    request.AllowAutoRedirect = true;
    request.KeepAlive = true;
    request.CookieContainer = new CookieContainer();

    response = (HttpWebResponse)request.GetResponse();
    response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
    cookies = response.Cookies;
    html = string.Empty;
    using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
    {
    html = reader.ReadToEnd();
    }

    //var doc = new HtmlDocument();
    //doc.LoadHtml(html);
    //HtmlNode ltNode = doc.DocumentNode.SelectSingleNode("//input[@name='lt']");
    //string lt = ltNode.Attributes["value"].Value;
    Match m = Regex.Match(html, @"<script>var lt = ""([^""]+)""", RegexOptions.IgnoreCase);
    string lt = m.Success ? m.Groups[1].Value : "";

    request = (HttpWebRequest)WebRequest.Create("http://47.97.109.186/cas/login");
    request.Method = "POST";
    request.Referer = "http://47.97.109.186/cas/login";
    request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3";
    request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36";
    request.ContentType = "application/x-www-form-urlencoded";
    request.AllowAutoRedirect = true;
    request.KeepAlive = true;
    request.CookieContainer = new CookieContainer();
    request.CookieContainer.Add(cookies);
    string postData = "username=3e4r5t6y&password=123456&_eventId=submit&lt=" + lt;
    byte[] postdatabyte = Encoding.UTF8.GetBytes(postData);
    request.ContentLength = postdatabyte.Length;
    using (Stream stream = request.GetRequestStream())
    {
    stream.Write(postdatabyte, 0, postdatabyte.Length);
    }
    response = (HttpWebResponse)request.GetResponse();
    response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);

    cookies = response.Cookies;
    html = string.Empty;
    using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
    {
    html = reader.ReadToEnd();
    }

  • 相关阅读:
    springboot项目引入通用mapper(tk.mybatis.mapper)的时候一些可能会踩的坑
    Zookeeper和Dubbo基本配置和调用
    Zookeeper和Dubbo介绍
    Git的基础命令大全,拉取推送,分支,合并,冲突
    Maven内置tomcat
    Maven高级,依赖传递和解决冲突,版本锁定,提取统一版本号
    SpringMvc的自定义异常,交给SpringMvc管理
    IDEA创建MavenWeb项目慢,问题根除
    Docker安装指南
    Docker容器入门
  • 原文地址:https://www.cnblogs.com/sheng9hhd/p/14100175.html
Copyright © 2020-2023  润新知