频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

利用HttpRequest登录到某个网站,然后获取网站信息的程序示例 [原创]

来源: 作者: 出处:巧巧读书 2006-09-18 进入讨论组

  问题:有的网站的相关内容必须要在登录后才可以查看,其登录信息保存在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;
               &nbs正文:http://www.qqread.com/dotnet/k234404.html 更多文章 更多内容请看网站服务器的选型专题,或进入讨论组讨论。
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
最新论坛文章
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章