zoukankan      html  css  js  c++  java
  • C#获得cookie的值

    //得到cookie值的方法

    [STAThread]    

    public static string RemoteLogin(string strLoginUrl, string strPostData, string cookieName)    

    {        

      CookieContainer cContainer=new CookieContainer();        

      byte[] data = System.Text.Encoding.ASCII.GetBytes(strPostData);        

      string bResult = "";

          HttpWebRequest myreq = (HttpWebRequest)WebRequest.Create(strLoginUrl);        

      myreq.Referer = strLoginUrl;        

      myreq.Method = "POST";        

      myreq.KeepAlive = true;        

      myreq.AllowAutoRedirect = false;        

      myreq.ContentLength = data.Length;        

       //cookieContainer = new CookieContainer();        

      myreq.CookieContainer = cContainer;        

      myreq.ContentType = "application/x-www-form-urlencoded";

            //这个在Post的时候,一定要加上,如果服务器返回错误,他还会继续再去请求,不会使用之前的错误数据,做返回数据                          

        myreq.ServicePoint.Expect100Continue = false;        

      HttpRequestCachePolicy noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);                             myreq.CachePolicy = noCachePolicy;

           try        

        {            

          Stream newStream = myreq.GetRequestStream();            

          newStream.Write(data, 0, data.Length);            

          newStream.Close();

                  HttpWebResponse myres = (HttpWebResponse)myreq.GetResponse();            

          StreamReader stream;            

          Encoding encoding;            

          if (!string.IsNullOrEmpty(myres.CharacterSet))            

          {                

            encoding = Encoding.GetEncoding(myres.CharacterSet);            

          }            

          else            

          {                

            encoding = Encoding.Default;            

          }            

          stream = new StreamReader(myres.GetResponseStream(), encoding);            

          string strHtml = stream.ReadToEnd();            

          //LogCommon.ilogDebug.Debug(strHtml);                

          if (myres.Cookies[cookieName] != null && !string.IsNullOrEmpty(myres.Cookies[cookieName].Value))                                            {                    

            bResult = myres.Cookies[cookieName].Value;                

          }

                    if (myreq != null)  myreq.Abort();            

             myres.Close();        

          }        

        catch (Exception ex)        

        {             throw ex;         }        

         return bResult;    

    }

    //调用

    RemoteLogin(请求的url,请求需要的参数,cookie的key);

  • 相关阅读:
    CS Academy Round #65 Count Arrays (DP)
    Codeforces Gym 101194C Mr. Panda and Strips(2016 EC-Final,区间DP预处理 + 枚举剪枝)
    Codeforces 915F Imbalance Value of a Tree(并查集)
    HDU 4866 Shooting (主席树)
    玲珑杯 Round #5 Problem E Tetration (枚举 + 欧拉公式)
    Codeforces 906D Power Tower(欧拉函数 + 欧拉公式)
    第十三届北航程序设计竞赛决赛网络同步赛 B题 校赛签到(建树 + 打标记)
    TopCoder SRM 722 Div1 Problem 600 DominoTiling(简单插头DP)
    Codeforces 901C Bipartite Segments(Tarjan + 二分)
    supervisor安装与使用
  • 原文地址:https://www.cnblogs.com/eryang/p/2659057.html
Copyright © 2011-2022 走看看