小菜来袭
2017-02-16 02:59
采纳率: 74.8%
浏览 2.6k

c# 向服务器post数据的时候 如何把参数和cookie一起发送

                System.Net.HttpWebRequest request;
                    request = (System.Net.HttpWebRequest)WebRequest.Create("http:www.xxxx.com");
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded; charset=utf-8";
                string paraUrlCoded = System.Web.HttpUtility.UrlEncode(textBox4.Text);
                paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode(textBox3.Text);
                byte[] payload;
                payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
                request.ContentLength = payload.Length;
                Stream writer = null;
                    writer = request.GetRequestStream();
                //将请求参数写入流
                writer.Write(payload, 0, payload.Length);
                // 关闭请求流
                writer.Close();
                System.Net.HttpWebResponse response = null;
                // 获得响应流
                    response = (System.Net.HttpWebResponse)request.GetResponse();
                System.IO.Stream s;
                s = response.GetResponseStream();
                StreamReader reader = new StreamReader(s);
                string strValue = reader.ReadToEnd();   //如果返回数据比较大,可以选择循环读取
                reader.Close();

如何发送cookie呢?
下面是火狐浏览器存储的cookie
PHPSESSID:"bjcjcq1q630hkmpjj5a65otev2"
_csrf:"91e0c51dd25fe28d57dcdb673c2b48a48ae843a820390ab8d8cc3f62ab9982b1a:2:{i:0;s:5:"_csrf";i:1;s:32:"TMuJAI2U_7A0qTdkNOKpTroujAdlL4s9";}"

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • 已采纳

    用cookiecontainer,你是要采集需要身份验证的吧。。。demo
    C#使用HttpWebRequest提交ASP.NET表单并保持Session和Cookie

    评论
    解决 无用
    打赏 举报
查看更多回答(2条)

相关推荐 更多相似问题