#region 同步通过POST方式发送数据 /// <summary> /// 通过POST方式发送数据 /// </summary> /// <param name="Url">url</param> /// <param name="postDataStr">Post数据</param> /// <param name="cookie">Cookie容器</param> /// <returns></returns> public string SendDataByPost(string Url, string postDataStr, ref CookieContainer cookie) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); if (cookie.Count == 0) { request.CookieContainer = new CookieContainer(); cookie = request.CookieContainer; } else { request.CookieContainer = cookie; } request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = postDataStr.Length; Stream myRequestStream = request.GetRequestStream(); StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312")); myStreamWriter.Write(postDataStr); myStreamWriter.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); return retString; } #endregion #region 同步通过GET方式发送数据 /// <summary> /// 通过GET方式发送数据 /// </summary> /// <param name="Url">url</param> /// <param name="postDataStr">GET数据</param> /// <param name="cookie">GET容器</param> /// <returns></returns> public string SendDataByGET(string Url, string postDataStr, ref CookieContainer cookie) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr); if (cookie.Count == 0) { request.CookieContainer = new CookieContainer(); cookie = request.CookieContainer; } else { request.CookieContainer = cookie; } request.Method = "GET"; request.ContentType = "text/html;charset=UTF-8"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); return retString; } #endregion private void button1_Click(object sender, EventArgs e) { string cookie = "BDUSS=xmTWI5bnNvVWRXUxxxxxxxxxMH42ckg1ak9VQm9zcG01N2xnSG94aGRZQVFBQUFBJCQAAAAAAAAAAAEAAACEh61i0~3B-rWl1dDN-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOg58FfoOfBXa2; BAIDUID=210F65A76356B6D480719A1A084D7D69:FG=1; PSTM=1476282093; BIDUPSID=9DDCF3D1EC83456525A2B69179CFA1D6; MCITY=-176%3A; PSINO=6; H_PS_PSSID=1465_21450_21108_21454_21394_21377_21192_21339; PHPSESSID=9c8cjtnb4l0210c62audgq64d4; Hm_lvt_4010fd5075fcfe46a16ec4cb65e02f04=1476059186,1478592650; Hm_lpvt_4010fd5075fcfe46a16ec4cb65e02f04=1478592650"; CookieContainer cc = new CookieContainer(); string url = "http://i.baidu.com/"; cc.SetCookies(new System.Uri(url), cookie); string content = SendDataByGET(url, "", ref cc); Console.WriteLine(content); Console.ReadLine(); }
网页登录账号后,通过360浏览器查看cookie
作者:黄聪
出处:http://www.cnblogs.com/huangcong/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://www.cnblogs.com/huangcong/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。