C# 调用rest接口 cookie的保持

之前调用一个系统的rest接口,该接口的调用方式是先登录获取cookie,然后将cookie作为后续请求的凭证。无奈只有java和php的样例代码。在网上找了很多解决方案,都没有实际解决问题。最后还是皇天不负有心人,找到了方法并测试有效,特地留存以备以后使用。

该方法主要是使用CookieContainer 来解决的。感谢该作者的文章解决了我的问题。


#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);



知行办公,专业移动办公平台 https://zx.naton.cn/
【总监】十二春秋之,3483099@qq.com;
【Master】zelo,616701261@qq.com;
【运营】狼行天下,897221533@qq.com;
【产品设计】流浪猫,364994559@qq.com;
【体验设计】兜兜,2435632247@qq.com;
【iOS】淘码小工,492395860@qq.com;iMcG33K,imcg33k@gmail.com;
【Android】人猿居士,1059604515@qq.com;思路的顿悟,1217022114@qq.com;
【java】首席工程师MR_W,feixue300@qq.com;
【测试】土镜问道,847071279@qq.com;
【数据】喜乐多,42151960@qq.com;
【安全】保密,你懂的。


阅读更多

没有更多推荐了,返回首页