为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

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

2018-04-25 6页 doc 23KB 11阅读

用户头像

is_477730

暂无简介

举报
利用HttpRequest登录到某个网站,然后获取网站信息的程序示例利用HttpRequest登录到某个网站,然后获取网站信息的程序示例 问题: 有的网站的相关内容必须要在登录后才可以查看,其登录信息保存在session变量之中。 这样,使用asphttp等组件就难以正确得到所要的信息。 解决: 使用asp.net中的httprequest和httpresponse来实现。 要点: 1。 通过附加一个cookiecontainer到httprequest对象中,可以得到登录后返回的代表 SESSION ID的COOKIE。 见Login方法 2。 将此COOKIE包含在一个coo...
利用HttpRequest登录到某个网站,然后获取网站信息的程序示例
利用HttpRequest登录到某个网站,然后获取网站信息的程序示例 问: 有的网站的相关必须要在登录后才可以查看,其登录信息保存在session变量之中。 这样,使用asphttp等组件就难以正确得到所要的信息。 解决: 使用asp.net中的httprequest和httpresponse来实现。 要点: 1。 通过附加一个cookiecontainer到httprequest对象中,可以得到登录后返回的代 SESSION ID的COOKIE。 见Login 2。 将此COOKIE包含在一个cookiecontainer中并附加到另一个HTTPREQUEST请求中, 则可以实现SESSION的还原。见getPage方法 源程序如下: getHttpInfo.aspx: 复制ASPX代码保存代码<%@ Page Language="c#" Codebehind="getHttpInfo.aspx.cs" AutoEventWireup="false" Inherits="PdfTest.getHttpInfo" %> WebForm1
<%@ Page Language="c#" Codebehind="getHttpInfo.aspx.cs" AutoEventWireup="false" Inherits="PdfTest.getHttpInfo" %> WebForm1
getHttpInfo.aspx.cs: 复制C#代码保存代码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 description for WebForm1. /// 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(";, "Action=&USERID=&Password="); } strResult = getPage(";, "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))); 转载请附带本文地址:
/
本文档为【利用HttpRequest登录到某个网站,然后获取网站信息的程序示例】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索