C#带cookie Post和Get方式发送数据,保持cookie

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#带cookie Post和Get方式发送数据,保持cookie相关的知识,希望对你有一定的参考价值。

在实际编程中,可能需要读取特定网页的信息,但很多网站需要用户登录后,才能够获取相关的页面内容,这就需要编程者先临时存储当前的cookie,
在C#中可以使用CookieContainer 对象来保存登录后的Cookie信息,这样,在每次发送数据的时候,附加上Cookie信息,就可以了。
#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

 

 

 

操作代码

CookieContainer cc=new CookieContainer();
cc.Add(new System.Uri("http://www.baidu.com "), new Cookie("phpSESSID", "xx"));
string content =  web.SendDataByGET("http://www.baidu.com/xx/yy.php","","gbk",ref cc);
MessageBox.Show(content);

 


 

以上是关于C#带cookie Post和Get方式发送数据,保持cookie的主要内容,如果未能解决你的问题,请参考以下文章

请求库之requests库

C#用HttpClient类post get,怎么设置cookies

C#带cookie发送http请求,http请求带cookie

用php做个post提交

get,post怎么获取cookie

C# 向网站post参数并存储cookie