System.Net.Cookie和System.Web.HttpCookie有什么区别,怎么相互转换。急!

webcookie 2005-03-04 03:36:06
我得到了一个System.Net.CookieCollection的实例cc,发现它是包含System.Net.Cookie的容器

foreach(System.Net.Cookie c in ccol)
{
Response.Cookies.Add(c);//我要输出这些Cookie,结果发现,支持的输出是System.Web.HttpCookie
//错误是指定的转换无效。谁知道System.Net.Cookie和System.Web.HttpCookie有什么区别,怎么相互转换。急!
}
...全文
1316 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
chinasqf 2005-03-08
  • 打赏
  • 举报
回复
mark
Montaque 2005-03-08
  • 打赏
  • 举报
回复
向浏览这个服务器上web页面的那个客户端写Cookie。

直接用script就可以

比如向会客户端请求中多一个cookie

Document.Cookie="Newcookie=Value"
webcookie 2005-03-08
  • 打赏
  • 举报
回复
你可以通过我上面说的方法把这个cookie发回服务器端,而不能发向浏览这个服务器上web页面的另一个客户端。如果我理解有错的话请原谅!




我就是要向浏览这个服务器上web页面的那个客户端写Cookie。
这是saucer老大说的思路,但没有给方法。所以我才出来问。(学艺不精,没办法)。
难道还要去问saucer老大。
webcookie 2005-03-08
  • 打赏
  • 举报
回复
把其中一个tostring(),然后赋值给另外一个不可以吗????

怎么越说越复杂?


我也知道ToString(),就怕这样转换会丢失一些东西。
好吧,我先试一下,不行的话再去找saucer
syeerzy 2005-03-08
  • 打赏
  • 举报
回复
把其中一个tostring(),然后赋值给另外一个不可以吗????

怎么越说越复杂?
lovelxj 2005-03-08
  • 打赏
  • 举报
回复
重写输出的部分应该可以实现咯
webdiyer 2005-03-07
  • 打赏
  • 举报
回复
System.Net.Cookie本来就是在客户端程序中存在的,不能说再发送到客户端,不知道你是怎么从a.aspx中获得System.Net.Cookie这个实例的?
nga96 2005-03-07
  • 打赏
  • 举报
回复
UP
webdiyer 2005-03-07
  • 打赏
  • 举报
回复
你可以通过我上面说的方法把这个cookie发回服务器端,而不能发向浏览这个服务器上web页面的另一个客户端。如果我理解有错的话请原谅!
webdiyer 2005-03-07
  • 打赏
  • 举报
回复
如果你是通过webrequest获取的cookie,那么使用这个方法的程序就是上面所指的客户端了,假如你是通过一个web项目或winform项目从另一台服务器用你所说的方法获取到cookie,那么获取cookie的这个项目所在的就是客户端了
webcookie 2005-03-07
  • 打赏
  • 举报
回复
就是用webrequest,你不是也使那样获得的吗。

webcookie 2005-03-06
  • 打赏
  • 举报
回复
说的就是,我当然知道是System.Web.HttpCookie,可我要把a.aspx得到的这个
System.Net.Cookie写到客户端去。

怎么做我就不会了。
webcookie 2005-03-06
  • 打赏
  • 举报
回复
这么说吧。
比如你写的那一段程序是a.aspx
我现在要把这个Cookie写到,浏览此页(a.aspx)的客户机上。
webdiyer 2005-03-06
  • 打赏
  • 举报
回复
System.Net.Cookie 本来就是客户端程序管理的Cookie,写到客户端去是什么意思呢?

==================
www.webdiyer.com
webdiyer 2005-03-06
  • 打赏
  • 举报
回复
这么说吧。
比如你写的那一段程序是a.aspx
我现在要把这个Cookie写到,浏览此页(a.aspx)的客户机上。

=================>

那应该是用System.Web.HttpCookie,而不是System.Net.Cookie
webcookie 2005-03-06
  • 打赏
  • 举报
回复
up
wjcking 2005-03-05
  • 打赏
  • 举报
回复
System.Net.Cookie是表示客户端程序通过http响应接收到的Cookie,而System.Web.HttpCookie是服务器端通过http请求从客户端获取的Cookie,比如下面这个winform程序通过向指定的url发送包含cookie的web请求然后获取返回的响应的代码:

WebRequestObject = (HttpWebRequest) WebRequest.Create("http://www.webdiyer.com/cookietest.aspx");
WebRequestObject.Method = "GET";

System.Net.CookieContainer CookieContainerObject = new System.Net.CookieContainer();
System.Net.Cookie Cookie = new System.Net.Cookie();

Cookie.Name = "userid";
Cookie.Value = "1234567890";
Cookie.Domain = Request.ServerVariables["HTTP_HOST"];
Cookie.Secure = true;
CookieContainerObject.Add(Cookie);

WebRequestObject.CookieContainer = CookieContainerObject;
WebResponseObject = (HttpWebResponse) WebRequestObject.GetResponse();
sr = new StreamReader(WebResponseObject.GetResponseStream());

String Results = sr.ReadToEnd();

webcookie 2005-03-05
  • 打赏
  • 举报
回复
up
webcookie 2005-03-05
  • 打赏
  • 举报
回复
bitsbird(一瓢 单身走我路)
看不懂。

webdiyer(陕北吴旗娃)
我功力不够,怎么能把这个System.Net.Coolie写到客户端去呢。

cuike519(marshal(修练中...))
不太了解,自己写不出。
zipo 2005-03-05
  • 打赏
  • 举报
回复
mark
加载更多回复(7)
特别说明请注意: 根许多网友反应, using Maticsoft.Functions; 这些代码看不懂 其实Functions 这个dll是我定义常用的函数类,如果需要跟我联系索取http://sql8.net 下面有我的群号, 其中 ArtsShop.Model.Arts_Product _p = new ArtsShop.Model.Arts_Product(); ArtsShop.BLL.Arts_Product p = new ArtsShop.BLL.Arts_Product(); _p = p.GetModel(id); 这是我的商品信息的类,三层结构,这个在用时你们只能换成你们自己的,这些代码完全可以删除, 比如 MyDr[1] = _p.Title; 用来读取商品名的,你们可以改MyDr[1] = dr["productname"].ToString();就行了, AddToCart.aspx页面代码 无标题页 保存 取消 编辑 继续购物 清空购物车 下订单 AddToCart.aspx.cs页面代码 using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using Maticsoft.Functions;public partial class AddToCart : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { int ProID; HttpCookie cookie; bool Tempbl = false; string Tempstr; if (!Page.IsPostBack) { if (!object.Equals(Request.QueryString["id"], null)) { ProID = int.Parse(Request.QueryString["id"]); //创购物车cookie yxy .//sql8.net if (object.Equals(Request.Cookies["ztbscart"], null)) cookie = new HttpCookie("ztbscart"); else cookie = Request.Cookies["ztbscart"]; //判断是否已存在于购物车内 yxy // sql8.net for (int i = 0; i < cookie.Values.Keys.Count; i++) { if (!object.Equals(cookie.Values.Keys[i], null)) { Tempstr = cookie.Values.AllKeys[i].ToString(); if (Tempstr.Trim() != "") { if (ProID == int.Parse(cookie.Values.AllKeys[i])) { Tempbl = true; break; } } } } //不未购买过则加入购物车 yxy //sql8.net if (!Tempbl) cookie.Values.Add(ProID.ToString(), "1"); else { } TimeSpan ts = new TimeSpan(0, 0, 10, 0); cookie.Expires = DateTime.Now + ts; Response.AppendCookie(cookie); } BindGrid(); } } //绑定数据 yxy //sql8.net private void BindGrid() { DataTable MyDt; DataRow MyDr; string str = ""; MyDt = new DataTable(); MyDt.Columns.Add(new DataColumn("id", str.GetType())); MyDt.Columns.Add(new DataColumn("Title", str.GetType())); MyDt.Columns.Add(new DataColumn("Num", str.GetType())); MyDt.Columns.Add(new DataColumn("Price", str.GetType())); MyDt.Columns.Add(new DataColumn("Discount", str.GetType())); MyDt.Columns.Add(new DataColumn("Vipprice", str.GetType())); MyDt.Columns.Add(new DataColumn("Totle", str.GetType())); if (!object.Equals(Request.Cookies["ztbscart"], null)) { HttpCookie cookie = Request.Cookies["ztbscart"]; double Totle; //Response.Write("|" + Request.Cookies["ztbscart"].Values.Keys[1].ToString() + "|"); //Response.End(); for (int i = 0; i < cookie.Values.Keys.Count; i++) { int id; MyDr = MyDt.NewRow(); if (cookie.Values.AllKeys[i] != "" && cookie.Values[i] != "") { id = int.Parse(cookie.Values.AllKeys[i].ToString()); ArtsShop.Model.Arts_Product _p = new ArtsShop.Model.Arts_Product(); ArtsShop.BLL.Arts_Product p = new ArtsShop.BLL.Arts_Product(); _p = p.GetModel(id); MyDr[0] = id; MyDr[1] = _p.Title; MyDr[2] = cookie.Values[i]; MyDr[3] = _p.Price; MyDr[4] = _p.Discount; MyDr[5] = _p.Vipprice1; Totle = double.Parse(MyDr[2].ToString()) * double.Parse(MyDr[5].ToString()); MyDr[6] = Totle; MyDt.Rows.Add(MyDr); } } GridView1.DataSource = MyDt.DefaultView; GridView1.DataBind(); } } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { //编辑某行数量 yxy //sql8.net GridView1.EditIndex = e.NewEditIndex; BindGrid(); } protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { //取消更新 yxy //sql8.net GridView1.EditIndex = -1; BindGrid(); } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { //更新数量 yxy //sql8.net string num; TextBox tempbx = new TextBox(); Label templb = new Label(); tempbx = (TextBox)(GridView1.Rows[e.RowIndex].Cells[6]).Controls[1]; num = tempbx.Text.ToString(); HttpCookie cookie = new HttpCookie("ztbscart"); for (int i = 0; i < GridView1.Rows.Count; i++) { string id; string tempnum; id = GridView1.Rows[i].Cells[1].Text.ToString(); if (e.RowIndex == i) tempnum = num; else { templb = (Label)(GridView1.Rows[i].Cells[6]).Controls[1]; tempnum = templb.Text.ToString(); } if (tempnum.Trim() == "") tempnum = "0"; //Response.Write("ID:"+id.ToString() + "Num:"+tempnum+":"+i+"");//测试用途 yxy//sql8.net cookie.Values.Add(id, tempnum); } //Response.End(); TimeSpan ts = new TimeSpan(0, 0, 10, 0); cookie.Expires = DateTime.Now + ts; Response.AppendCookie(cookie); GridView1.EditIndex = -1; Message.GoTo("AddToCart.aspx"); } protected void LinkButton4_Click(object sender, EventArgs e) { //继续购物 yxy //sql8.net Message.WebClose(); } protected void LinkButton3_Click(object sender, EventArgs e) { //清空购物车 yxy //sql8.net CheckBox tempcb = new CheckBox(); HttpCookie cookie = new HttpCookie("ztbscart"); Label templb = new Label(); for (int i = 0; i < GridView1.Rows.Count; i++) { tempcb = (CheckBox)(GridView1.Rows[i].Cells[0]).Controls[1]; if (!tempcb.Checked) { string id; string tempnum; id = GridView1.Rows[i].Cells[1].Text.ToString(); templb = (Label)(GridView1.Rows[i].Cells[6]).Controls[1]; tempnum = templb.Text.ToString(); if (tempnum.Trim() == "") tempnum = "0"; //Response.Write("ID:"+id.ToString() + "Num:"+tempnum+":"+i+"");//测试用途 yxy//sql8.net cookie.Values.Add(id, tempnum); } } TimeSpan ts = new TimeSpan(0, 0, 10, 0); cookie.Expires = DateTime.Now + ts; Response.AppendCookie(cookie); Message.GoTo("AddToCart.aspx"); } protected void CheckAll_CheckedChanged(object sender, EventArgs e) { //全选事件 yxy //sql8.net CheckBox tempcb = new CheckBox(); bool tempbl; tempcb = (CheckBox)(GridView1.HeaderRow.Cells[0]).Controls[1]; tempbl = tempcb.Checked; for (int i = 0; i < GridView1.Rows.Count; i++) { tempcb = (CheckBox)(GridView1.Rows[i].Cells[0]).Controls[1]; tempcb.Checked = tempbl; } } } _________________________________________________________________________ 如转载请注明原出处 www.sql8.net

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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