using System; 
using System.Net; 
using System.IO; 
using System.Text;

// 
// TODO: 在此处添加代码以启动应用程序 
// 
string url="http://localhost/csdn2/1.asp"; 
// <% 
// if request("aa")="zhuye" then session("ok")="ok" 
// if session("ok")="ok" then 
// response.write("登录") 
// else 
// response.write("没有登录") 
// end if 
// %> 
string indata="aa=zhuye"; 
string outdata=""; 
CookieContainer myCookieContainer=new CookieContainer(); 
//新建一个CookieContainer来存放Cookie集合 
HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create(url); 
//新建一个HttpWebRequest 
myHttpWebRequest.ContentType="application/x-www-form-urlencoded"; 
myHttpWebRequest.ContentLength=indata.Length; 
myHttpWebRequest.Method="POST"; 
myHttpWebRequest.CookieContainer=myCookieContainer; 
//设置HttpWebRequest的CookieContainer为刚才建立的那个myCookieContainer 
Stream myRequestStream=myHttpWebRequest.GetRequestStream(); 
StreamWriter myStreamWriter=new StreamWriter(myRequestStream,Encoding.GetEncoding("gb2312")); 
myStreamWriter.Write(indata); 
//把数据写入HttpWebRequest的Request流 
myStreamWriter.Close(); 
myRequestStream.Close(); 
//关闭打开对象 
HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse(); 
//新建一个HttpWebResponse 
myHttpWebResponse.Cookies=myCookieContainer.GetCookies(myHttpWebRequest.RequestUri); 
//获取一个包含url的Cookie集合的CookieCollection 
Stream myResponseStream=myHttpWebResponse.GetResponseStream(); 
StreamReader myStreamReader=new StreamReader(myResponseStream,Encoding.GetEncoding("gb2312")); 
outdata=myStreamReader.ReadToEnd(); 
//把数据从HttpWebResponse的Response流中读出 
myStreamReader.Close(); 
myResponseStream.Close(); 
Console.WriteLine(outdata); 
//显示"登录"

//拿到了Cookie,再进行请求就能直接读取到登录后的内容了 
myHttpWebRequest=(HttpWebRequest)WebRequest.Create(url); 
myHttpWebRequest.CookieContainer=myCookieContainer;//* 
//刚才那个CookieContainer已经存有了Cookie,把它附加到HttpWebRequest中则能直接通过验证 
myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse(); 
myHttpWebResponse.Cookies=myCookieContainer.GetCookies(myHttpWebRequest.RequestUri); 
myResponseStream=myHttpWebResponse.GetResponseStream(); 
myStreamReader=new StreamReader(myResponseStream,Encoding.GetEncoding("gb2312")); 
outdata=myStreamReader.ReadToEnd(); 
myStreamReader.Close(); 
myResponseStream.Close(); 
Console.WriteLine(outdata); 
//再次显示"登录" 
//如果把*行注释调,就显示"没有登录" 
}

发表于:2008-06-13 00:36:403楼 得分:30
本地已 经有的cookies 
在3.0以下平台:使用api 
[DllImport("wininet.dll", SetLastError=true)] 
public static extern bool InternetGetCookie(string url, string cookieName, StringBuilder cookieData, ref int size); 
在3.0以上平台可以使用 
System.Windows.Application.GetCookie方法。

【C#】获取本地Cookie的问题的更多相关文章

  1. taotao购物车2 解决购物车本地cookie和服务器redis不同步的问题

    下面的思路逻辑一定要理清楚,比较绕 思路; 前面已经实现了在cookie本地维护购物车的功能, 这次加入和服务器同步功能, 因为 购物车 操作比较频繁,所以,后台服务器 用redis存储用户的购物车信 ...

  2. 浅谈 php 采用curl 函数库获取网页 cookie 和 带着cookie去访问 网页的方法!!!!

    由于近段时间帮朋友开发一个能够查询正方教务系统的微信公众平台号.有所收获.这里总结下个人经验. 开讲前,先吐槽一下新浪云服务器,一个程序里的   同一个函数  在PC测试可以正常运行,在它那里就会挂的 ...

  3. js设置、获取单值cookie和多值cookie

    js设置.获取单值cookie和多值cookie,代码如下: var CookieUtil = (function () { var Cookie = function () { // 获取单值coo ...

  4. android获取本地图片并显示图片

    import java.io.FileNotFoundException; import android.content.ContentResolver; import android.content ...

  5. Linux编程获取本地IP

    #include <stdio.h> #include <sys/types.h> #include <ifaddrs.h> #include <netine ...

  6. Java获取本地IP地址

    import java.net.InetAddress; import java.net.UnknownHostException; public class IpTest { public stat ...

  7. 清除Cookie、获取指定Cookie的值、添加一个Cookie(24小时过期)、添加一个Cookie

    MXS&Vincene  ─╄OvЁ  &0000007 ─╄OvЁ  MXS&Vincene MXS&Vincene  ─╄OvЁ:今天很残酷,明天更残酷,后天很美好 ...

  8. Android开发之获取本地视频和获取自拍视频

    1.获取本地所有视频 public void getLoadMedia() { Cursor cursor = UILApplication.instance.getApplicationContex ...

  9. 获取本地IP地址信息

    2012-06-05    /// <summary>         /// 获取本地IP地址信息         /// </summary>         void G ...

随机推荐

  1. INTERRUPT CONTROLLER

    1,中断的基本概念 CPU与外设之间传输数据的控制方式通常有3种:查询方式,中断方式和DMA方式.查询方式的优点是硬件开销小不需要额外的硬件支持只是通过软件不断的轮询,使用起来也就比较简单,但在此方式 ...

  2. Ubuntu 14.04 SSH + 远程登录xrdp

    1. 安装ssh 打开"终端窗口",输入"sudo apt-get install openssh-server"-->回车-->输入"y ...

  3. 【CSS3】Advanced4:Advanced Colors

    1.rgba(red,green,blue,alpha(不透明度0.0(完全透明)与 1.0(完全不透明)) 2.HSLa(hue(色调 0red 120green 240blue),saturati ...

  4. POJ 1321 棋盘问题 DFS搜索

    简单搜索 练习一下回溯 #include <iostream> #include <cstdio> #include <cstring> #include < ...

  5. WordPress更新服务加快收录

    WordPress更新服务的设置在后台"设置"-"撰写".更新服务下的文本框中默认为http://rpc.pingomatic.com/.该地址网站Ping-o ...

  6. redis在.NET下的使用

    windows SEVER包:http://code.google.com/p/servicestack/wiki/RedisWindowsDownload windows仅用来测试,性能不如在lin ...

  7. WeUI首页、文档和下载 - 专为微信设计的 UI 库 - 开源中国社区

    Download Bitnami Review Board Stack click here WeUI首页.文档和下载 - 专为微信设计的 UI 库 - 开源中国社区

  8. [MySQL]快速解决"is marked as crashed and should be repaired"故障

    具体报错如下: Table '.\Tablename\posts' is marked as crashed and should be repaired 提示说论坛的帖子表posts被标记有问题,需 ...

  9. poj 3281 Dining【拆点网络流】

    Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11828   Accepted: 5437 Descripti ...

  10. [置顶] 自己写sqlhelper类

    自己写sqlhelper类 using System; using System.Collections.Generic; using System.Configuration; using Syst ...