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

None.gif using  System;
None.gif
using  System.Net;
None.gif
using  System.IO;
None.gif
using  System.Text;

None.gif         [STAThread]
None.gif        
static   void  Main( string [] args)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
//
InBlock.gif            
// TODO: 在此处添加代码以启动应用程序
InBlock.gif            
//
InBlock.gif
            string url="http://localhost/csdn2/1.asp";
InBlock.gif
//            <%
InBlock.gif
//            if request("aa")="zhuye" then session("ok")="ok"
InBlock.gif
//            if session("ok")="ok" then
InBlock.gif
//            response.write("登录")
InBlock.gif
//            else
InBlock.gif
//            response.write("没有登录")
InBlock.gif
//            end if
InBlock.gif
//            %>
InBlock.gif
            string indata="aa=zhuye";
InBlock.gif            
string outdata="";
InBlock.gif            CookieContainer myCookieContainer
=new CookieContainer();
InBlock.gif            
//新建一个CookieContainer来存放Cookie集合
InBlock.gif
            HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create(url);
InBlock.gif            
//新建一个HttpWebRequest
InBlock.gif
            myHttpWebRequest.ContentType="application/x-www-form-urlencoded";
InBlock.gif            myHttpWebRequest.ContentLength
=indata.Length;
InBlock.gif            myHttpWebRequest.Method
="POST";
InBlock.gif            myHttpWebRequest.CookieContainer
=myCookieContainer;
InBlock.gif            
//设置HttpWebRequest的CookieContainer为刚才建立的那个myCookieContainer
InBlock.gif
            Stream myRequestStream=myHttpWebRequest.GetRequestStream();
InBlock.gif            StreamWriter myStreamWriter
=new StreamWriter(myRequestStream,Encoding.GetEncoding("gb2312"));                
InBlock.gif            myStreamWriter.Write(indata);
InBlock.gif            
//把数据写入HttpWebRequest的Request流
InBlock.gif
            myStreamWriter.Close();
InBlock.gif            myRequestStream.Close();
InBlock.gif            
//关闭打开对象
InBlock.gif
            HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
InBlock.gif            
//新建一个HttpWebResponse
InBlock.gif
            myHttpWebResponse.Cookies=myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);
InBlock.gif            
//获取一个包含url的Cookie集合的CookieCollection
InBlock.gif
            Stream myResponseStream=myHttpWebResponse.GetResponseStream();
InBlock.gif            StreamReader myStreamReader
=new StreamReader(myResponseStream,Encoding.GetEncoding("gb2312"));
InBlock.gif            outdata
=myStreamReader.ReadToEnd();
InBlock.gif            
//把数据从HttpWebResponse的Response流中读出
InBlock.gif
            myStreamReader.Close();
InBlock.gif            myResponseStream.Close();
InBlock.gif            Console.WriteLine(outdata);
InBlock.gif            
//显示"登录"
InBlock.gif
InBlock.gif            
//拿到了Cookie,再进行请求就能直接读取到登录后的内容了
InBlock.gif
            myHttpWebRequest=(HttpWebRequest)WebRequest.Create(url);
InBlock.gif            myHttpWebRequest.CookieContainer
=myCookieContainer;//*
InBlock.gif            
//刚才那个CookieContainer已经存有了Cookie,把它附加到HttpWebRequest中则能直接通过验证
InBlock.gif
            myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
InBlock.gif            myHttpWebResponse.Cookies
=myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);            
InBlock.gif            myResponseStream
=myHttpWebResponse.GetResponseStream();
InBlock.gif            myStreamReader
=new StreamReader(myResponseStream,Encoding.GetEncoding("gb2312"));
InBlock.gif            outdata
=myStreamReader.ReadToEnd();
InBlock.gif            myStreamReader.Close();
InBlock.gif            myResponseStream.Close();
InBlock.gif            Console.WriteLine(outdata);    
InBlock.gif            
//再次显示"登录"
InBlock.gif            
//如果把*行注释调,就显示"没有登录"
ExpandedBlockEnd.gif
        }

注释写得很明确了,不多罗嗦了。
补充:如果是以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";即可。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值