/// <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, ref string content, int i) { CookieContainer cookie = new CookieContainer(); HttpWebRequest httpRequest = InitWebRequest(postString, postUrl, method, cookie); HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();//获得 服务端响应 content = "登录成功"; using (Stream responseStream = httpResponse.GetResponseStream()) { StreamReader sr = new StreamReader(responseStream, System.Text.Encoding.UTF8); string returnStr = sr.ReadToEnd(); if (returnStr.Contains("用户名或者密码不正确")) { content = "用户名或者密码不正确"; } } string date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); string hang = "\r\n" + "-----------------------------------\r\n"; content = hang + "第" + i + "登录:" + date + ":" + content; CommonTools.WriteTxt(writepath, content); return(cookie);//拿到cookie }
/// <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 }