• (winform cookie)如何向某网址Post信息,并得到CookieContainer以便以后直接通过验证


    using System;
    using System.Net;
    using System.IO;
    using System.Text;
    class Test
    {
    static void Main(string[] args)
    {
    string url = "链接的URL";
    string indata = "aa=zhuye";
    string outdata = "";
    CookieContainer myCookieContainer
    = new CookieContainer();
    //新建一个CookieContainer来存放Cookie集合
    HttpWebRequest myHttpWebRequest = (HttpWebRequest) WebRequest.Create(url);
    //新建一个HttpWebRequest
    myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
    myHttpWebRequest.ContentLength
    = indata.Length;
    myHttpWebRequest.Method
    = "POST";
    myHttpWebRequest.CookieContainer
    = myCookieContainer;
    //设置HttpWebRequest的CookieContainer为刚才建立的那个myCookieContainer
    Stream myRequestStream = myHttpWebRequest.GetRequestStream();
    StreamWriter myStreamWriter
    = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
    myStreamWriter.Write(indata);
    //把数据写入HttpWebRequest的Request流
    myStreamWriter.Close();
    myRequestStream.Close();
    //关闭打开对象
    HttpWebResponse myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse();
    //新建一个HttpWebResponse
    myHttpWebResponse.Cookies = myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);
    //获取一个包含url的Cookie集合的CookieCollection
    Stream myResponseStream = myHttpWebResponse.GetResponseStream();
    StreamReader myStreamReader
    = new StreamReader(myResponseStream, Encoding.GetEncoding("gb2312"));
    outdata
    = myStreamReader.ReadToEnd();
    //把数据从HttpWebResponse的Response流中读出
    myStreamReader.Close();
    myResponseStream.Close();
    Console.WriteLine(outdata);
    //显示"登录"
    //拿到了Cookie,再进行请求就能直接读取到登录后的内容了
    myHttpWebRequest = (HttpWebRequest) WebRequest.Create(url);
    myHttpWebRequest.CookieContainer
    = myCookieContainer;//*
    //刚才那个CookieContainer已经存有了Cookie,把它附加到HttpWebRequest中则能直接通过验证
    myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse();
    myHttpWebResponse.Cookies
    = myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);
    myResponseStream
    = myHttpWebResponse.GetResponseStream();
    myStreamReader
    = new StreamReader(myResponseStream, Encoding.GetEncoding("gb2312"));
    outdata
    = myStreamReader.ReadToEnd();
    myStreamReader.Close();
    myResponseStream.Close();
    Console.WriteLine(outdata);
    //再次显示"登录"
    //如果把*行注释调,就显示"没有登录"
    }
    };
    ";

    补充:如果是以get方式登录的话,直接修改url就可以了,indate可以不写任何东西。
    (不要去修改 myHttpWebRequest.Method为GET),
    比如把asp文件修改为 if request.querystring("aa")="zhuye" then session("ok")="ok",
    只要修改url为 string url="http://localhost/csdn2/1.asp?aa=zhuye";即可。 

    心中时常装有一盘人生的大棋,天作棋盘,星作棋子,在斗转星移中,只有不断地搏击人生,人生才有意义,生命才能彰显光辉,才能收获一分永恒。
  • 相关阅读:
    Nginx 对访问量的控制
    Shell入门
    小程序如何处理键盘覆盖输入框
    移动端适配之二:visual viewport、layout viewport和ideal viewport介绍
    javascript-对象
    bind函数作用、应用场景以及模拟实现
    「面试题」如何实现一个圣杯布局?
    一款轮播组件的诞生
    超火js库: Lodash API例子
    js 中的 number 为何很怪异
  • 原文地址:https://www.cnblogs.com/top5/p/2267319.html
Copyright © 2020-2023  润新知