关于C#保存cookie的问题

haixin3036 2014-01-25 04:16:43
用C#实现本地编辑文章,提交网站后台发布的功能。 cookie功能不能实现,代码如下,问题出在哪呢?


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;

namespace cookie登陆
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

public CookieContainer cook = new CookieContainer();

private void button1_Click(object sender, EventArgs e)
{
string url = "http://localhost/admin/login.php";
byte[] data = Encoding.UTF8.GetBytes("gotopage=%2Fjianongadmin%2F&dopost=login&adminstyle=newdedecms&userid=admin&pwd=admin&sm1=");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url));

//定义表头信息
request.CookieContainer = cook;//cookie容器

request.Method = "POST";
request.Accept = "text/html, application/xhtml+xml, */*";
request.Referer = "http://localhost/admin/login.php?gotopage=%2Fjianongadmin%2F";
request.Headers.Add("Accept-Language", "zh-CN");
request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)";
request.ContentType = "application/x-www-form-urlencoded";
request.Host = "www.goodfarmer.com";
request.ContentLength = data.Length;

request.Headers.Add("Cache-Control", "no-cache");
//提交数据流
Stream s = request.GetRequestStream();
s.Write(data, 0, data.Length);

//处理返回数据
HttpWebResponse resphone = (HttpWebResponse)request.GetResponse();
StreamReader read =new StreamReader( resphone.GetResponseStream());
textBox1.Text = read.ReadToEnd();
UpdateCookie(ref cook, resphone.Cookies); //写入cookie



}


private void button2_Click(object sender, EventArgs e) //通过保存的Cookie访问发布文章的后台页面
{
string url = "http://localhost/admin/article_add.php?channelid=1&cid=0";

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url));

request.CookieContainer = cook;

request.Method = "GET";
request.Accept = "text/html, application/xhtml+xml, */*";
request.Headers.Add("Accept-Language", "zh-CN");
request.Accept = "image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
request.Headers.Add("Accept-Language", "zh-cn");
request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)";
request.Host = "www.goodfarmer.com";

HttpWebResponse respone =(HttpWebResponse) request.GetResponse();
StreamReader read = new StreamReader(respone.GetResponseStream(),Encoding.UTF8);

textBox1.Text = read.ReadToEnd();


}


/// <summary>
/// 更新Cookie方法
/// </summary>
/// <param name="mycookie"></param>
/// <param name="newcookie">获取的新cookie</param>
static void UpdateCookie(ref CookieContainer mycookie, CookieCollection newcookie)
{


if (newcookie.Count > 0)
{
foreach (Cookie coo in newcookie)
{
mycookie.Add(coo);

}
}

}


}
}


...全文
178 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
Regan-lin 2014-01-25
  • 打赏
  • 举报
回复
断点看看这个条件if (newcookie.Count > 0)这里不

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧