(论坛答疑点滴)如何向某网址Post信息,并得到CookieContainer以便以后直接通过验证

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

        [STAThread]
        
static void Main(string[] args)
        
{
            
//
            
// 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);    
            
//再次显示"登录"
            
//如果把*行注释调,就显示"没有登录"
        }

注释写得很明确了,不多罗嗦了。
补充:如果是以get方式登录的话,直接修改url就可以了,indate可以不写任何东西。(不要去修改myHttpWebRequest.Method为GET),比如把asp文件修改为if request.querystring("aa")="zhuye" then session("ok")="ok",只要修改url为string url="http://localhost/csdn2/1.asp?aa=zhuye";即可。
posted @ 2005-04-22 13:26 lovecherry 阅读(4976) 评论(7)  编辑 收藏 网摘 所属分类: webform

  回复  引用  查看    
#1楼 2005-05-08 11:38 | interim      
不错是不错
不过,你打开一下yahoo奇摩,登陆实施
就不行了吧?
QQ
77504839

  回复  引用  查看    
#2楼 2005-05-11 22:33 | jackhu      
using System;
using System.IO;
using System.Net;
using System.Text;


/// <summary>
/// Fetches a Web Page
/// </summary>
class WebFetch
{
static void Main(string[] args)
{
// used to build entire input
StringBuilder sb = new StringBuilder();

// used on each read operation
byte[] buf = new byte[8192];

// prepare the web page we will be asking for
HttpWebRequest request = (HttpWebRequest)
WebRequest.Create("http://www.mayosoftware.com");

// execute the request
HttpWebResponse response = (HttpWebResponse)
request.GetResponse();

// we will read data via the response stream
Stream resStream = response.GetResponseStream();

string tempString = null;
int count = 0;

do
{
// fill the buffer with data
count = resStream.Read(buf, 0, buf.Length);

// make sure we read some data
if (count != 0)
{
// translate from bytes to ASCII text
tempString = Encoding.ASCII.GetString(buf, 0, count);

// continue building the string
sb.Append(tempString);
}
}
while (count > 0); // any more data to read?

// print out page source
Console.WriteLine(sb.ToString());
}
}


  回复  引用    
#3楼 2005-11-06 18:23 | devil [未注册用户]
你好, 我发现如果登录页写入cookies后用Location转向,那么就不能取得cookie,大家有什么办法解决?
  回复  引用    
#4楼 2006-06-01 11:31 | Ivan_ [未注册用户]
对。这个cookie丢失的问题困扰了我很久。:(
  回复  引用    
#5楼 2006-06-10 18:07 | norock [未注册用户]
to devil
to Ivan_

禁止链接的自动重定向,而是每次都检测响应报头是否Location重定向。
另外取得新 CookieCollection 后不应该直接覆盖原有的. 而应遍历cookie项比较更新当前值项(或追加新项).
;-)

PS: 不要滥用
PS: 更不要骂我~~~ 只因为报头中的cookie实在不是安全的东西。

  回复  引用    
#6楼 2007-09-09 14:59 | Invest [未注册用户]
就是这样的,post过去获取而已,有的网站有特殊处理,没有办法
  回复  引用    
#7楼 2007-09-14 21:56 | HR_Guy [未注册用户]
最近在写东西想把Gmail的地址簿导出,可是好像登陆及url转向的问题很头疼,一直没做好

我也是这样用的,可是不行啊,汗


发表评论



姓名 [登录] [注册] 
主页
Email (仅博主可见) 
验证码 *  验证码看不清,换一张
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论   新用户注册   返回页首      

导航: 网站首页 社区 新闻 博问 闪存 网摘 招聘 .NET频道 知识库 找找看 Google站内搜索



China-pub 计算机图书网上专卖店!6.5万品种 2-8折!
China-Pub 计算机绝版图书按需印刷服务

相关文章:

相关链接: