zoukankan      html  css  js  c++  java
  • 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();
    }

  • 相关阅读:
    DoNet:浅淡对delegate的理解
    纹理学习总结
    Vue引发的getter和setter
    JS——判断一个对象是否为空
    使用vue-axios请求geoJson数据报错的问题
    在vue-cli项目中使用echarts
    Vue中的$set的使用
    史上最全的开发和设计资源大全(转--------来源:伯乐在线)
    IOS safari浏览器登陆时Cookie无法保存的问题
    JS中const、var 和let的区别
  • 原文地址:https://www.cnblogs.com/sheng9hhd/p/14100175.html
Copyright © 2011-2022 走看看