分享
 
 
 

HttpRequest获取网站信息的程序示例

王朝html/css/js·作者佚名  2006-11-24
窄屏简体版  字體: |||超大  

问题:有的网站的相关内容必须要在登录后才可以查看,其登录信息保存在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.

[1] [2] 下一页

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有