代码如下:
string _loginContent,_loginURL;// _loginURL="http://www.cmfu.com/loginuser.asp";
// _loginContent="user_name=@username&pass_word=@password&ekey=&user_type=1";
_loginURL="http://www.nch.com.tw/login.php";
_loginContent="key=login&orig_self=%2Fmybook.php&orig_query_string=&login_id=@username&login_pw=@password";
string UserName="vstest";
string PassWord="123456";
byte[] LoginContent = Encoding.ASCII.GetBytes(_loginContent.Replace("@username",UserName).Replace("@password",PassWord)); HttpWebResponse LoginResponse;
HttpWebRequest LoginRequest = (HttpWebRequest)WebRequest.Create(_loginURL);
LoginRequest.Method="POST";
LoginRequest.ContentType="application/x-www-form-urlencoded";
LoginRequest.ContentLength=LoginContent.Length;
LoginRequest.CookieContainer=new CookieContainer();
Stream streamContent=LoginRequest.GetRequestStream();
streamContent.Write(LoginContent,0,LoginContent.Length);
streamContent.Close(); LoginRequest.GetResponse();现在遇到的问题是,当登陆cmfu时一切正常,LoginRequest.CookieContainer中就存有Cookie,可以用于访问书架,而登陆nch时则LoginRequest.CookieContainer中为空,这是怎么回事?
用Sniffer Pro可以看到,POST后确实返回了Cookie,用IE登录的话也确实有Cookie存在,为什么程序会得不到求教
谢谢

解决方案 »

  1.   

    cmfu...
    一年前给朋友写了个刷积分的.
      

  2.   

    LoginRequest.CookieContainer=new CookieContainer();
    这个要保存到一个全局变量
    第二次Request的时候赋值过来才行
      

  3.   

    Red_angelX(八戒) ( ) 信誉:100    Blog   加为好友  2007-5-16 16:10:59  得分: 0  
    LoginRequest.CookieContainer=new CookieContainer();
    这个要保存到一个全局变量
    第二次Request的时候赋值过来才行这没用啊
    跟上面最后再加一句CookieContainer cc=LoginRequest.CookieContainer
    效果一样吧
    至少cmfu的就没区别,我在正式的程序里就是这么做的,完全可以
    但这里nch的问题是最后出来LoginRequest.CookieContainer.Count=0
    ...
      “一年前给朋友写了个刷积分的.”等想做的都做完了可以考虑作为附加功能~~~
      

  4.   

    TO:randb(从大二下学期开始不再迷茫!!狂学编程!!) 
    好的,如果今天还解决不了,回家后加你qq
    不过我对这个可是一窍不通,要请你多指点了~~~还有没有哪位能有直接、方便点的解决方法啊?
    到底是什么原因引起的呢
      

  5.   

    HttpWebRequest LoginRequest = (HttpWebRequest)WebRequest.Create(_loginURL);
    ------------------------------------------------------------------------------------
    HttpWebRequest LoginRequest = (HttpWebRequest)WebRequest.Create(new System.Uri((_loginURL));用Sniffer Pro看到的POST的数据是这个吗??
    _loginContent="key=login&orig_self=%2Fmybook.php&orig_query_string=&login_id=@username&login_pw=@password";
      

  6.   

    To:zhangliu_521(浪客) 
    不会复制 ,截了2张图
    http://photo.163.com/photos/wuliaoing_000/52301200/
      

  7.   

    看不到..
    再给你个我收藏的资料参考.
    最近有个项目需要从网络上下载网页信息和文件,并且需要登录后才能下载,所以做了个下载的通用类,供大家参考。这个是文件下载类:
     using System;
     using System.Net;
     using System.Web;
     
     public class SRWebClient
      {
     CookieContainer cookie;
     public SRWebClient()
      {
     cookie=new CookieContainer();
     }
     
      /**//// <TgData>
     /// <Alias>下载Web源代码</Alias>
     /// </TgData>
     public string DownloadHtml(string URL)
      {
     HttpWebRequest request=HttpWebRequest.create(URL) as HttpWebRequest;
     request.CookieContainer=cookie;
     request.AllowAutoRedirect=false;
     
     WebResponse res=request.GetResponse();
     string r="";
     
     System.IO.StreamReader S1 = new System.IO.StreamReader(res.GetResponseStream(), System.Text.Encoding.Default );
     try
      {
     r = S1.ReadToEnd();
     }
     catch(Exception er)
      {
     Log l=new Log();
     l.writelog("下载Web错误",er.ToString());
     }
     finally
      {
     res.Close();
     S1.Close();
     }
     
     return r;
     }
     
      /**//// <TgData>
     /// <Alias>下载文件</Alias>
     /// </TgData>
     public long DownloadFile(string FileURL,string FileSavePath)
      {
     long Filelength=0;
     HttpWebRequest req=HttpWebRequest.create(FileURL) as HttpWebRequest;
     
     req.CookieContainer=cookie;
     req.AllowAutoRedirect=true;
     
     HttpWebResponse res=req.GetResponse() as HttpWebResponse;
     System.IO.Stream stream=res.GetResponseStream();
     try
      {
     Filelength=res.ContentLength;
     
     byte [] b=new byte[512];
     
     int nReadSize=0;
     nReadSize=stream.Read(b,0,512);
     
     System.IO.FileStream fs= System.IO.File.create(FileSavePath);
     try
      {
     while(nReadSize >0)
      {
     fs.Write(b,0,nReadSize);
     nReadSize=stream.Read(b,0,512);
     }
     }
     finally
      {
     fs.Close();
     }
     }
     catch(Exception er)
      {
     Log l=new Log();
     l.writelog("下载文件错误",er.ToString());
     }
     finally
      {
     res.Close();
     stream.Close();
     }
     
     return Filelength;
     }
      

  8.   


      /**//// <TgData>
     /// <Alias>提交数据</Alias>
     /// </TgData>
     public void Request(string RequestPageURL,RequestData Data)
      {
     string StrUrl=RequestPageURL;
     HttpWebRequest request=HttpWebRequest.create(StrUrl) as HttpWebRequest;
     HttpWebResponse response;
     
     string postdata=Data.GetData();
     request.Referer=RequestPageURL;
     request.AllowAutoRedirect=false;
     request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.01; Windows NT 5.0)";
     request.CookieContainer = cookie;
     
     Uri u=new Uri(StrUrl);
     
     if (postdata.Length > 0 ) //包含要提交的数据 就使用Post方式
      {
     request.ContentType = "application/x-www-form-urlencoded"; //作为表单请求
     request.Method = "POST"; //方式就是Post
     
     //把提交的数据换成字节数组
     Byte [] B = System.Text.Encoding.Default.GetBytes(postdata);
     request.ContentLength = B.Length;
     
     System.IO.Stream SW = request.GetRequestStream(); //开始提交数据
     SW.Write(B, 0, B.Length);
     SW.Close();
     }
     
     response = request.GetResponse() as HttpWebResponse;
     response.Close();
     }
     
     
     }
     
    这个是提交的数据类:
     using System.Collections;
     using System.IO;
     
     public class RequestData
      {
     ArrayList arr=new ArrayList();
     public RequestData()
      {
     
     }
     
     public string GetData()
      {
     string r="";
     
     for(int i=0;i<arr.Count;i++)
      {
     data d=(data)arr[i];
     if(r.Length>0)r+="&";
     r+=d.Field+"="+d.Value;
     }
     return r;
     }
     
     public void AddField(string Field,string Value)
      {
     data a=new data();
     a.Field=Field;
     a.Value=Value;
     
     arr.Add(a);
     }
     
     struct data
      {
     public string Field,Value;
     }
     
     
     }
      

  9.   

    谢谢,我先看看图片地址:
    http://img.photo.163.com/vKC5QwZUTCN1e6u3TwtXWA==/153122387331382002.jpg
    http://img.photo.163.com/TPnZJglE9PFesPKJtHurcA==/153122387331382000.jpg
      

  10.   


    看到了
    是那样的格式的啊
    再给你一个
    慧聪网登录地址:http://b2b.hc360.com/rewrite-url/www/dl.html
    用户名:mytestcs
    密  码:aabbccdd
    string url = "http://my.b2b.hc360.com/my/turbine/template/firstview,other_login.html";string indata = @"LoginID=mytestcs&Passwd=aabbccdd&LoginChk=true&Submit=%B5%C7%A1%A1%A1%A1%C2%BC";            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);
      

  11.   

    这段代码和我的似乎区别不大啊
    // string url = "http://my.b2b.hc360.com/my/turbine/template/firstview,other_login.html";
    //
    // string indata = @"LoginID=mytestcs&Passwd=aabbccdd&LoginChk=true&Submit=%B5%C7%A1%A1%A1%A1%C2%BC"; string url = "http://www.cmfu.com/loginuser.asp"; string indata = @"user_name=vstest&pass_word=123456&ekey=&user_type=1";// string url = "http://www.nch.com.tw/login.php";
    //
    // string indata = @"key=login&orig_self=%2Fmybook.php&orig_query_string=&login_id=vstest&login_pw=123456";url和indata分别用2个网站的代替,在Console.WriteLine(outdata); 处设断点,可以看到
    hc和cmfu的myCookieContainer.Count均大于0,而nch的myCookieContainer.Count=0
    cmfu第二次GetResponseStream的时候会提示"您登录过了,用不着再次登录",而nch的则每次都会有输入用户名密码的框出来
    <table width=100% border=1 cellspacing=0 bgcolor=#E7D6C3 height=60 bordercolorlight=#000000 bordercolordark=#FFFFFF>\r\n          <form name=mlogin method=post action=login.php onSubmit=\"return check_mlogin_value()\">\r\n          <input type=hidden name=key value=login>\r\n          <input type=hidden name=orig_self value=/mybook.php>\r\n          <input type=hidden name=orig_query_string value=login=1>\r\n\t        <tr bgcolor=#CCCCD6>\r\n\t          <td class=a>&nbsp;际嘿<input type=text name=login_id size=10 class=n></td>\r\n\t        </tr>\r\n\t        <tr bgcolor=#CCCCD6>\r\n\t          <td class=a>&nbsp;盞絏<input type=password name=login_pw size=10 class=n></td>\r\n\t        </tr>\r\n\t        <tr bgcolor=#CCCCD6>\r\n\t          <td class=a>\r\n\t\t      &nbsp;<input type=submit value=祅 class=a>\r\n\
    t\t      <input type=button value=爹 class=a onClick=javascript:document.location.replace('join.php')>\r\n\t\t      <input type=button value=а癘盞絏 class=a onClick=javascript:document.location.replace('forget.php')>\r\n\t          </td>\r\n\t        </tr>\r\n\t      </form>\r\n\t      </table>
      

  12.   

    UP我另外还有一个问题,不知道有没有人能帮帮忙http://community.csdn.net/Expert/TopicView.asp?id=5499315
    (其实就是书和章节这些东西之间的关系)
      

  13.   

    上午试了下,发现禁止AllowAutoRedirect后,myHttpWebResponse.Headers里有
    Set-Cookies
    但是CookieContainer里却没有东西
    通过人工处理后就可用了但是为什么无法自动获取,人工处理毕竟比较麻烦啊:(