運用C#在採集時進行自動驗證登入

來源:互聯網
上載者:User
最近有朋友在問我如何進行資訊採集時對一些有使用者或和密碼驗證的網站進行採集,剛好最近在項目中有運用到這個來進行驗證登入,將部分代碼發出來與大家分享 學習,這隻是我在網上參考人家的做的一個雛形試用版,雖然能用,但是效能可能不是那麼好。。目前只能對那些沒有登入驗證碼的網站有效,如果有驗證碼的,呵 呵。那還得去寫一個分析驗證的類來進行Get.....

最近我的工作比較雜亂,也沒有來得及整理,這些都是臨時用了一下就擱在一邊了,最近又忙著去學習Ubuntu、然後.Net+MySQL的一個項目,呵呵。還有星際。

首先開啟網站,查看源檔案,找到他的登入表單部分。

比如:
<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

下面是C#模仿登入程式:Login.cs

using System;
using System.Data;
using System.Net;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

/**//// <summary>
 /// 登入網站並擷取Cookies
 /// </summary>
 /// <returns>成功登入的Cookie資訊</returns>
public static CookieContainer Get_Login()
 ...{
            CookieContainer cc = new CookieContainer();
            string FormURL="http://blog.hnce.net/loginMain.jsp";                //處理表單的絕對URL地址
            string FormData = "username=slick&passwd=hedy12345";    //表單需要提交的參數,注意改為你登入的資訊。
            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] data = encoding.GetBytes(FormData);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(FormURL);
            request.Method = "POST";    //資料提交方式
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = data.Length;
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)";
            //類比一個UserAgent
            Stream newStream = request.GetRequestStream();
            newStream.Write(data, 0, data.Length);

            newStream.Close();
                   
            request.CookieContainer = cc;
                   
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            cc.Add(response.Cookies);
            Stream stream = response.GetResponseStream();
            string WebContent = new StreamReader(stream, System.Text.Encoding.Default).ReadToEnd();
            return cc;
}

調用以上的方法來擷取需要登入才能查看的內容。

            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即為你登入之後看到的內容。程式寫的比較淩亂,如你有什麼意見和不清楚的地方可以給我發郵件:hedongyang#gmail.com
(#請換為@就可以了) 
 

以上為轉自:http://blog.csdn.net/hedongyang/archive/2007/05/15/1609379.aspx

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.