动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
  
   .Net → 阅读文章

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

作者来源: 
阅读 数 333 人次 , 2006-3-29 4:33:00 


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

}
}

 
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:用ASP.Net写一个发送ICQ信息的程序
· 下一篇:ASP.NET窗体对话框的实现
· Asp.net 页面导航的几种方法与比较
· 最佳ASP.NET编程习惯
· 10天学会ASP.net之第二天
· 再议正则表达式(这次是在asp.net 上的应用)
· Java.NET --一个基于Java的Microsoft.NET框架的实现


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077