欢迎光临
我们一直在努力

利用HttpRequest登录到某个网站,然后获取网站信息的程序示例 [原创]-.NET教程,Asp.Net开发

建站超值云服务器,限时71元/月

问题:有的网站的相关内容必须要在登录后才可以查看,其登录信息保存在session变量之中。这样,使用asphttp等组件就难以正确得到所要的信息。

解决:使用asp.net中的httprequest和httpresponse来实现。

要点:
1。 通过附加一个cookiecontainer到httprequest对象中,可以得到登录后返回的代表session id的cookie。 见login方法
2。 将此cookie包含在一个cookiecontainer中并附加到另一个httprequest请求中,则可以实现session的还原。见getpage方法

源程序如下:

gethttpinfo.aspx:
<%@ page language="c#" codebehind="gethttpinfo.aspx.cs" autoeventwireup="false" inherits="pdftest.gethttpinfo" %>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
<html>
    <head>
        <title>webform1</title>
        <meta content="microsoft visual studio 7.0" name="generator">
        <meta content="c#" name="code_language">
        <meta content="javascript" name="vs_defaultclientscript">
        <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetschema">
    </head>
    <body>
        <form id="form1" method="post" runat="server">
        </form>
    </body>
</html>

gethttpinfo.aspx.cs:
using system;
using system.collections;
using system.componentmodel;
using system.data;
//using system.data.oledb;
using system.drawing;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
using system.net;
using system.io;
using system.text;
using system.text.regularexpressions;
using microsoft.data.odbc;

namespace pdftest
{
    /// <summary>
    /// summary description for webform1.
    /// </summary>
    public class gethttpinfo : system.web.ui.page
    {
        protected static string cookieheader;

    
        private void page_load(object sender, system.eventargs e)
        {
            // put user code to initialize the page here

            string strresult;

            if (httpcontext.current.application["cookieheader"] != null)
            {
                cookieheader = (string)httpcontext.current.application["cookieheader"];
            }
            else
            {
                //login into the website and keep the cookie for the session in the application variable
                string strlogin = login("http://www.thesiteyouwanttovisit/theloginpage.asp", "action=&userid=&password=") ;
            }
            

            strresult = getpage("http://www.thesiteyouwanttovisit/theloginpage.asp", "action=&data=") ;

            //write the result to htm file
            filestream htmfile = new filestream("c:\save.htm", filemode.openorcreate);
            streamwriter sw = new streamwriter(htmfile);
            sw.write(strresult);
            sw.close();
            htmfile.close();

            // output the result
            response.write(strresult);
        }

        public static string login(string url, string paramlist)
        {
            httpwebresponse res = null;
            string strresult="";

            try
            {

                httpwebrequest req = (httpwebrequest)webrequest.create(url);
                req.method = "post";
                req.contenttype = "application/x-www-form-urlencoded";
                req.allowautoredirect = false;
                cookiecontainer cookiecon = new cookiecontainer();
                req.cookiecontainer = cookiecon;

                stringbuilder urlencoded = new stringbuilder();
                char[] reserved = {?, =, &};
                byte[] somebytes = null;

                if (paramlist != null)
                {
                    int i=0, j;
                    while(i<paramlist.length)
                    {
                        j=paramlist.indexofany(reserved, i);
                        if (j==-1)
                        {
                            urlencoded.append(httputility.urlencode(paramlist.substring(i, paramlist.length-i)));
                            break;
                        }
                        urlencoded.append(httputility.urlencode(paramlist.substring(i, j-i)));
                        urlencoded.append(paramlist.substring(j,1));
                        i = j+1;
                    }
                    somebytes = encoding.utf8.getbytes(urlencoded.tostring());
                    req.contentlength = somebytes.length;
                    stream newstream = req.getrequeststream();
                    newstream.write(somebytes, 0, somebytes.length);
                    newstream.close();
                }
                else
                {
                    req.contentlength = 0;
                }

                res = (httpwebresponse)req.getresponse();
                cookieheader = req.cookiecontainer.getcookieheader(new uri(url));
                httpcontext.current.application.lock();
                httpcontext.current.application["cookieheader"] = cookieheader;
                httpcontext.current.application.unlock();

                stream receivestream = res.getresponsestream();
                encoding encode = system.text.encoding.getencoding("utf-8");
                streamreader sr = new streamreader( receivestream, encode );
                char[] read = new char[256];
                int count = sr.read( read, 0, 256 );
                while (count > 0)
                {
                    string str = new string(read, 0, count);
                    strresult += str;
                    count = sr.read(read, 0, 256);
                }
            }
            catch(exception e)
            {
                strresult = e.tostring();
            }
            finally
            {
                if ( res != null )
                {
                    res.close();
                }
            }

            return strresult;
        }

        public static string getpage(string url, string paramlist)
        {
            httpwebresponse res = null;
            string strresult = "";

            try
            {

                httpwebrequest req = (httpwebrequest)webrequest.create(url);
                req.method = "post";
                req.keepalive = true;
                req.contenttype = "application/x-www-form-urlencoded";
                cookiecontainer cookiecon = new cookiecontainer();
                req.cookiecontainer = cookiecon;
                req.cookiecontainer.setcookies(new uri(url),cookieheader);
                stringbuilder urlencoded = new stringbuilder();
                char[] reserved = {?, =, &};
                byte[] somebytes = null;

                if (paramlist != null)
                {
                    int i=0, j;
                    while(i<paramlist.length)
                    {
                        j=paramlist.indexofany(reserved, i);
                        if (j==-1)
                        {
                            urlencoded.append(httputility.urlencode(paramlist.substring(i, paramlist.length-i)));
                            break;
                        }
                        urlencoded.append(httputility.urlencode(paramlist.substring(i, j-i)));
                        urlencoded.append(paramlist.substring(j,1));
                        i = j+1;
                    }
                    somebytes = encoding.utf8.getbytes(urlencoded.tostring());
                    req.contentlength = somebytes.length;
                    stream newstream = req.getrequeststream();
                    newstream.write(somebytes, 0, somebytes.length);
                    newstream.close();
                }
                else
                {
                    req.contentlength = 0;
                }

                res = (httpwebresponse)req.getresponse();
                stream receivestream = res.getresponsestream();
                encoding encode = system.text.encoding.getencoding("utf-8");
                streamreader sr = new streamreader( receivestream, encode );
                char[] read = new char[256];
                int count = sr.read( read, 0, 256 );
                while (count > 0)
                {
                    string str = new string(read, 0, count);
                    strresult += str;
                    count = sr.read(read, 0, 256);
                }
            }
            catch(exception e)
            {
                strresult = e.tostring();
            }
            finally
            {
                if ( res != null )
                {
                    res.close();
                }
            }

            return strresult;
        }

        #region web form designer generated code
        override protected void oninit(eventargs e)
        {
            //
            // codegen: this call is required by the asp.net web form designer.
            //
            initializecomponent();
            base.oninit(e);
        }
        
        /// <summary>
        /// required method for designer support – do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void initializecomponent()
        {    
            this.load += new system.eventhandler(this.page_load);

        }
        #endregion

        

    }
}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 利用HttpRequest登录到某个网站,然后获取网站信息的程序示例 [原创]-.NET教程,Asp.Net开发
分享到: 更多 (0)

相关推荐

  • 暂无文章