haixin3036 2014年01月25日
关于C#保存cookie的问题
用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);

}
}

}


}
}


...全文
114 点赞 收藏 1
写回复
1 条回复

还没有回复,快来抢沙发~

发动态
发帖子
C#
创建于2007-09-28

8.4w+

社区成员

64.0w+

社区内容

.NET技术 C#
社区公告
暂无公告