利用HttpRequest
日期:2004年3月29日 作者:清风网络学院 查看:[大字体 中字体 小字体]-
利用HttpRequest
利用HttpRequest登录到某个网站,然后获取网站信息的程序示例 问题:有的网站的相关内容必须要在登录后才可以查看,其登录信息保存在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 herestring 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}
}(出处:急速软件下载学院)
-
- 利用HttpRequest 相关文章:
- ·利用HttpRequest登录到某个网站,然后获取网站信息的程序示例
- ·利用HttpRequest
- 利用HttpRequest 相关软件
- 特别声明:本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作
- 者.文章版权归文章原始作者所有.对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转
- 载的文章有版权问题请联系编辑人员,我们尽快予以更正. 转载请注明来源:http://www.hackhome.com
下一篇:把图象文件转换成XML格式文件
精品推荐
热点TOP10
- ·Telnet高级入侵攻略及原理
- ·最详细的黑客入门法则
- ·Windows黑客编程基础
- ·从零开始学黑客:网络黑客新手入门指南
- ·菜鸟都可以入侵一个网吧的所有电脑
- ·我是怎么进入他人计算机的
- ·菜鸟变黑客高手
- ·黑客入侵前的信息收集
- ·实例讲解如何对目标进行ARP欺骗
- ·木马是如何启动的
- ·流光5.0使用小窍门
- ·经典:各个网络端口的入侵方法
- ·最好的入侵linux教程
- ·流光破解email帐号教程
- ·多种DDoS攻击技术方法
- ·理想已经滑落——“中国黑客”10年嬗变
- ·黑客破解电脑密码实用技巧大全!
- ·入侵命令详解
- ·网络钓鱼攻击的新标志:DNS攻击
- ·教你如何:入侵UNIX和Linux服务器入门
特别推荐
- ·Telnet高级入侵攻略及原理
- ·多种DDoS攻击技术方法
- ·黑客入侵前的信息收集
- ·看黑客如何发动对电子邮件系统的攻击
- ·经典:各个网络端口的入侵方法
- ·防范黑客 从两大方面阻止域名被劫持
- ·黑客、红客、蓝客、飞客究竟是什么?
- ·认清虚虚实实的“ARP攻击”防御方法
- ·黑客知识 彻底堵死SQL注入工具漏洞
- ·赛迪网"IT技术百家讲坛"开讲 如何防御黑客?
- ·黑客如何利用DNS欺骗在局域网中挂马
- ·Windows黑客编程基础
- ·黑客很狡猾 如何减轻DDoS攻击危害?
- ·网络攻击的几种攻击与防御手法大揭秘
- ·黑客攻击的几种常用手法及防御技巧
- ·经典常见计算机密码破解实用手册
- ·经典总结:各个网络端口的入侵
- ·最详细的黑客入门法则
- ·攻防技巧PHP数据库安全之SQL注入
- ·如何有效的抵抗DDOS
