首先打开网站,查看源文件,找到他的登录表单部分。
比如:
<form name="login" action="loginMain.jsp" method="POST" target="_top">
      <table width="218" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td width="50" height="28" class="hui"><div align="right">用户名:</div></td>
          <td width="168" height="28"><input class="hui" id="username"
            maxlength="40" size="23" name="username" id="username" /></td>
        </tr>
        <tr>
          <td height="28" class="hui"><div align="right">密 码:</div></td>
          <td height="28"><input class="hui" id="passwd"
            maxlength="40" size="23" name="passwd" type="password" id="passwd" /></td>
        </tr>
      </table>
</form>
从以上表单可以看出,表单提交的方法是:POST,提交至loginMain.jsp处理,共有两个表单项即:username、passwd
 1using System;
 2using System.Data;
 3using System.Net;
 4using System.Text;
 5using System.IO;
 6using System.Text.RegularExpressions;
 7
 8/**//// <summary>
 9 /// 登录网站并获取Cookies
10 /// </summary>
11 /// <returns>成功登录的Cookie信息</returns>

12public static CookieContainer Get_Login()
13 {
14            CookieContainer cc = new CookieContainer();
15            string FormURL="http://blog.hnce.net/loginMain.jsp";                //处理表单的绝对URL地址
16            string FormData = "username=slick&passwd=hedy12345";    //表单需要提交的参数,注意改为你已注册的信息。
17            ASCIIEncoding encoding = new ASCIIEncoding();
18            byte[] data = encoding.GetBytes(FormData);
19
20            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(FormURL);
21            request.Method = "POST";    //数据提交方式
22            request.ContentType = "application/x-www-form-urlencoded";
23            request.ContentLength = data.Length;
24            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)";
25            //模拟一个UserAgent
26            Stream newStream = request.GetRequestStream();
27            newStream.Write(data, 0, data.Length);
28
29            newStream.Close();
30                   
31            request.CookieContainer = cc;
32                   
33            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
34            cc.Add(response.Cookies);
35            Stream stream = response.GetResponseStream();
36            string WebContent = new StreamReader(stream, System.Text.Encoding.Default).ReadToEnd();
37            return cc;
38}
调用以上的方法来获取需要登录才能查看的内容。
            CookieContainer cc = new CookieContainer();
            cc 
= Login.Get_Login();            //获取登录Cookies

            
string PhotoClassURL = "http://blog.hnce.net/xxx.jsp";
            HttpWebRequest Myrequest 
= (HttpWebRequest)WebRequest.Create(PhotoClassURL);
            Myrequest.CookieContainer 
= cc;
            HttpWebResponse Myresponse 
= (HttpWebResponse)Myrequest.GetResponse();
            cc.Add(Myresponse.Cookies);
            Stream Mystream 
= Myresponse.GetResponseStream();
            
string sHtml = new StreamReader(Mystream, System.Text.Encoding.Default).ReadToEnd();
sHtml即为你登录之后看到的内容.
posted on 2007-05-15 14:54  Jeri  阅读(1045)  评论(1)    收藏  举报