zoukankan      html  css  js  c++  java
  • HttpWebRequest模拟登陆,存储Cookie以便登录请求后使用

    [一篮饭特稀原创,转载请注明出自http://www.cnblogs.com/wanghafan/p/3284481.html]

    PostLogin :登录,并保存Cookie

    复制代码
     1  public static string PostLogin(string postData, string requestUrlString, ref CookieContainer cookie)
     2     {
     3         ASCIIEncoding encoding = new ASCIIEncoding();
     4         byte[] data = encoding.GetBytes(postData);
     5         //向服务端请求
     6         HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(requestUrlString);
     7         myRequest.Method = "POST";
     8         myRequest.ContentType = "application/x-www-form-urlencoded";
     9         myRequest.ContentLength = data.Length;
    10         myRequest.CookieContainer = new CookieContainer();
    11         Stream newStream = myRequest.GetRequestStream();
    12         newStream.Write(data, 0, data.Length);
    13         newStream.Close();
    14         //将请求的结果发送给客户端(界面、应用)
    15         HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
    16         cookie.Add(myResponse.Cookies);
    17         StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
    18         return reader.ReadToEnd();
    19     }
    复制代码

    PostRequest :登录后使用Cookie进行其他操作

    复制代码
     1  public static string PostRequest(string postData, string requestUrlString, CookieContainer cookie)
     2     {
     3         ASCIIEncoding encoding = new ASCIIEncoding();
     4         byte[] data = encoding.GetBytes(postData);
     5         HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(requestUrlString);
     6         myRequest.Method = "POST";
     7         myRequest.ContentType = "application/x-www-form-urlencoded";        
     8         myRequest.ContentLength = data.Length;
     9         myRequest.CookieContainer = cookie;
    10         Stream newStream = myRequest.GetRequestStream();
    11         newStream.Write(data, 0, data.Length);
    12         newStream.Close();
    13         HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
    14         StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
    15         return reader.ReadToEnd();
    16     }
    复制代码

    e.g.

    复制代码
    1             string strIMSPhone = tb_IMSPhone.Text.Trim();
    2             string strIMSPwd = tb_IMSPwd.Text.Trim();
    3             string postData = "username=" + strIMSPhone + "&password=" + strIMSPwd + "&type=2";
    4             CookieContainer cookie=new CookieContainer();
    5             if (IMSHelper.PostLogin(postData, post_signIn, ref cookie).Equals("ok"))
    6             {
    7                 string strCont = PostRequest("", post_getContJsonData, cookie);    
    8             }
    复制代码
  • 相关阅读:
    43前端
    42 前端
    python 列表
    python 字符串方法
    python while语句
    zhy2_rehat6_mysql02
    zhy2_rehat6_mysql01
    bay——安装_Oracle 12C-RAC-Centos7.txt
    bay——RAC_ASM ORA-15001 diskgroup DATA does not exist or is not mounted.docx
    bay——Oracle RAC集群体系结构.docx
  • 原文地址:https://www.cnblogs.com/ok519/p/3488091.html
Copyright © 2011-2022 走看看