コード例 #1
0
ファイル: CommonTools.cs プロジェクト: nik2011/NikTest
        /// <summary>
        /// 获取cookie
        /// </summary>
        /// <param name="postString">请求的数据</param>
        /// <param name="postUrl">请求的路径</param>
        /// <param name="method">请求的方式</param>
        /// <returns></returns>
        public static CookieContainer GetCookie(string postString, string postUrl, string method, string referer = "", string host = "")
        {
            CookieContainer cookie       = new CookieContainer();
            HttpWebRequest  httpRequest  = CommonTools.InitWebRequest(postString, postUrl, method, cookie, "", referer, host);
            HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();//获得 服务端响应

            //string strCookie= httpRequest.CookieContainer.GetCookieHeader(new Uri(postUrl));
            cookie.Add(httpResponse.Cookies);
            string content = "";

            using (Stream responseStream = httpResponse.GetResponseStream())
            {
                StreamReader sr = new StreamReader(responseStream, System.Text.Encoding.UTF8);
                content = sr.ReadToEnd();
            }
            if (cookie != null && cookie.Count > 0)
            {
                content = "登录成功";
            }
            string date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            string hang = "\r\n" + "-----------------------------------\r\n";

            content = hang + date + ":" + content;
            CommonTools.WriteTxt(writepath, content);
            return(cookie);//拿到cookie
        }