Thursday, March 22, 2012

Take into a varible the HTML of a aspx page

Jacas,

You can use Response.Filter to do this. Here's an example:

using System;
using System.IO;

namespace cslocation
{
/// <summary>
/// Summary description for MyFilter.
/// </summary>
public class HTMLFilter : Stream
// This filter changes all characters passed through it to uppercase.
{
private Stream _sink;
private long _position;

public byte[] data;

public EmailFilter(Stream sink)
{
_sink = sink;
}

// The following members of Stream must be overriden.
public override bool CanRead
{
get { return true; }
}

public override bool CanSeek
{
get { return true; }
}

public override bool CanWrite
{
get { return true; }
}

public override long Length
{
get { return 0; }
}

public override long Position
{
get { return _position; }
set { _position = value; }
}

public override long Seek(long offset, System.IO.SeekOrigin direction)
{
return _sink.Seek(offset, direction);
}

public override void SetLength(long length)
{
_sink.SetLength(length);
}

public override void Close()
{
_sink.Close();
}

public override void Flush()
{
_sink.Flush();
}

public override int Read(byte[] buffer, int offset, int count)
{
return _sink.Read(buffer, offset, count);
}

// The Write method actually does the filtering.
public override void Write(byte[] buffer, int offset, int count)
{
data = new byte[count];
Buffer.BlockCopy(buffer, offset, data, 0, count);
_sink.Write (data, 0, count);
}

}

}

You can then instantiate this class as follows:

Response.Filter = new HTMLFilter(Response.Filter);

Hope that helps.

Jim Cheshire [MSFT]
Developer Support
ASP.NET
jamesche@dotnet.itags.org.online.microsoft.com

This post is provided as-is with no warranties and confers no rights.

-------
>Content-Class: urn:content-classes:message
>From: "jacas" <anonymous@dotnet.itags.org.discussions.microsoft.com>
>Sender: "jacas" <anonymous@dotnet.itags.org.discussions.microsoft.com>
>Subject: Take into a varible the HTML of a aspx page
>Date: Thu, 23 Oct 2003 09:00:01 -0700
>Lines: 7
>Message-ID: <00db01c3997e$b6ed2630$a301280a@dotnet.itags.org.phx.gbl>
>MIME-Version: 1.0
>Content-Type: text/plain;
>charset="iso-8859-1"
>Content-Transfer-Encoding: 7bit
>X-Newsreader: Microsoft CDO for Windows 2000
>X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
>Thread-Index: AcOZfrbt/w7LUNvpQrmKlBXOan6XQg==
>Newsgroups: microsoft.public.dotnet.framework.aspnet
>Path: cpmsftngxa06.phx.gbl
>Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:186086
>NNTP-Posting-Host: TK2MSFTNGXA11 10.40.1.163
>X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
>Somebody can expli-me how to take de the HTML of a aspx
>page into a string variable may be in de render or
>prerender event of a page
>
>thanksHy Jim,

I don't know if I don't understand but whith your code you
do a filter of the response but what I want is to take the
HTML an put it in a string varible... I do it from another
page like

you can get HtML froma a page like
mywebReq = WebRequest.Create("http://www.c-
sharpcorner.com/faq.asp")
mywebResp = mywebReq.GetResponse()
sr = New StreamReader(mywebResp.GetResponseStream,
System.Text.Encoding.ASCII)
strHTML = sr.ReadToEnd
TextBox1.Text = strHTML

but it has problems when the page is in de same directory
as de page in where de code is an in it you have to
athentificate if you want to wiev pages, but i thing it
might be a way of take that page if you are already
athentificate

i proof that
mywebReq = WebRequest.Create(site & "(" &
Request.ServerVariables("HTTP_ASPFILTERSESSIONID") & ")
page.aspx")

but d'ont works
and I thinkg there is a way for do that in the same page
that I want the HTML ...what i want is to take the
Response.content in a string variable like

str = Response.content (this is not possible but is for
expline what i want)

Thanks any way
Jacas,

I'm having a little trouble understanding exactly what you're saying, but
if I understand correctly, you want to get the HTML that is rendered by
ASP.NET and store it in a variable. If that's the case, Response.Filter is
the way to go and using the code I posted is what you want to use.

That code was originally written by one of my colleagues for a customer we
were working with who wanted to do the exact same thing you're doing. The
only difference is that our other customer was taking that HTML and
populating an e-mail message with it. You just want to store it. The end
result is the same. If you want to do it reliably, use Response.Filter.

Jim Cheshire [MSFT]
Developer Support
ASP.NET
jamesche@.online.microsoft.com

This post is provided as-is with no warranties and confers no rights.

-------
>Content-Class: urn:content-classes:message
>From: "jacas" <anonymous@.discussions.microsoft.com>
>Sender: "jacas" <anonymous@.discussions.microsoft.com>
>References: <00db01c3997e$b6ed2630$a301280a@.phx.gbl>
<SDvxA3YmDHA.1544@.cpmsftngxa06.phx.gbl>
>Subject: RE: Take into a varible the HTML of a aspx page
>Date: Fri, 24 Oct 2003 01:48:25 -0700
>Lines: 38
>Message-ID: <0ae701c39a0b$9627e2e0$a101280a@.phx.gbl>
>MIME-Version: 1.0
>Content-Type: text/plain;
>charset="iso-8859-1"
>Content-Transfer-Encoding: 7bit
>X-Newsreader: Microsoft CDO for Windows 2000
>X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
>Thread-Index: AcOaC5Yls6pRYc1aRouN+9Tsp3ecsA==
>Newsgroups: microsoft.public.dotnet.framework.aspnet
>Path: cpmsftngxa06.phx.gbl
>Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:186270
>NNTP-Posting-Host: TK2MSFTNGXA09 10.40.1.161
>X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
>Hy Jim,
>I don't know if I don't understand but whith your code you
>do a filter of the response but what I want is to take the
>HTML an put it in a string varible... I do it from another
>page like
>you can get HtML froma a page like
> mywebReq = WebRequest.Create("http://www.c-
>sharpcorner.com/faq.asp")
> mywebResp = mywebReq.GetResponse()
> sr = New StreamReader(mywebResp.GetResponseStream,
>System.Text.Encoding.ASCII)
> strHTML = sr.ReadToEnd
> TextBox1.Text = strHTML
>but it has problems when the page is in de same directory
>as de page in where de code is an in it you have to
>athentificate if you want to wiev pages, but i thing it
>might be a way of take that page if you are already
>athentificate
>i proof that
>mywebReq = WebRequest.Create(site & "(" &
>Request.ServerVariables("HTTP_ASPFILTERSESSIONID") & ")
>page.aspx")
>but d'ont works
>and I thinkg there is a way for do that in the same page
>that I want the HTML ...what i want is to take the
>Response.content in a string variable like
>str = Response.content (this is not possible but is for
>expline what i want)
>Thanks any way
I work with VB an i translate your code

Public Class HTMLFilter
Inherits Stream
Private _sink As Stream
Private _position As Long
Public data As Byte()

Public Sub EmailFilter(ByVal sink As Stream)
_sink = sink
End Sub
Overrides ReadOnly Property CanRead() As Boolean
Get
Return True
End Get
End Property

Overrides ReadOnly Property CanSeek() As Boolean
Get
Return True
End Get
End Property

Overrides ReadOnly Property CanWrite() As Boolean
Get
Return True
End Get
End Property
Overrides ReadOnly Property Length() As Long
Get
Return 0
End Get
End Property

Overrides Property Position() As Long
Get
Return _position
End Get
Set(ByVal Value As Long)
_position = Value
End Set
End Property

Public Overrides Function Seek(ByVal offset As
Long, ByVal direction As SeekOrigin) As Long
Return _sink.Seek(offset, direction)
End Function
Public Overrides Sub SetLength(ByVal length As
Long)
_sink.SetLength(length)
End Sub
Public Overrides Sub Close()
_sink.Close()
End Sub
Public Overrides Sub Flush()
_sink.Flush()
End Sub
Public Overloads Function Read(ByVal buffer() As
Byte, ByVal offset As Int16, ByVal count As Int16) As Int16
Return _sink.Read(buffer, offset, count)
End Function
Public Overloads Function Write(ByVal buffer() As
Byte, ByVal offset As Int16, ByVal count As Int16)
ReDim data(count)
System.Buffer.BlockCopy(buffer, offset, data,
0, count)
_sink.Write(data, 0, count)
End Function
End Class

but intelligence says to me that the class HTMLFilter must
either be declared 'MustInherit' or override the following
inherited 'MustOverride' members: Public Overridable
MustOverride Overloads Sub Write(buffer() As Byte, offset
As Integer, count As Integer), Public Overridable
MustOverride Overloads Function Read(buffer() As Byte,
offset As Integer, count As Integer) As Integer

but if I understand i do it... please if you can help me...
and if i understand the page is in de accesible punblic
data that is a byte() , sorry if it is very simple but how
i take it in a varible string , you say to instantiate the
class
Response.Filter= new HTMLFilter(Response.filter)

but how i can take the HTML in string (sorry because is
posible that i don't be a very good student) ;-)

Xavi
Xavi,

I can provide you with the sample, but if you need assistance in reworking
it for your application, you would need to open a case with us. That kind
of work goes beyond what we can do in the newsgroups.

Jim Cheshire [MSFT]
Developer Support
ASP.NET
jamesche@.online.microsoft.com

This post is provided as-is with no warranties and confers no rights.

-------
>Content-Class: urn:content-classes:message
>From: <anonymous@.discussions.microsoft.com>
>Sender: <anonymous@.discussions.microsoft.com>
>References: <00db01c3997e$b6ed2630$a301280a@.phx.gbl>
<SDvxA3YmDHA.1544@.cpmsftngxa06.phx.gbl>
<0ae701c39a0b$9627e2e0$a101280a@.phx.gbl>
<VFH0lAkmDHA.1772@.cpmsftngxa06.phx.gbl>
>Subject: RE: Take into a varible the HTML of a aspx page
>Date: Mon, 27 Oct 2003 10:52:48 -0800
>Lines: 89
>Message-ID: <026201c39cbb$83f450b0$a301280a@.phx.gbl>
>MIME-Version: 1.0
>Content-Type: text/plain;
>charset="iso-8859-1"
>Content-Transfer-Encoding: 7bit
>X-Newsreader: Microsoft CDO for Windows 2000
>Thread-Index: AcOcu4P0bnCG4ILqTTeyjwHdVOkP5A==
>X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
>Newsgroups: microsoft.public.dotnet.framework.aspnet
>Path: cpmsftngxa06.phx.gbl
>Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:186784
>NNTP-Posting-Host: TK2MSFTNGXA11 10.40.1.163
>X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
>I work with VB an i translate your code
>Public Class HTMLFilter
> Inherits Stream
> Private _sink As Stream
> Private _position As Long
> Public data As Byte()
> Public Sub EmailFilter(ByVal sink As Stream)
> _sink = sink
> End Sub
> Overrides ReadOnly Property CanRead() As Boolean
> Get
> Return True
> End Get
> End Property
> Overrides ReadOnly Property CanSeek() As Boolean
> Get
> Return True
> End Get
> End Property
> Overrides ReadOnly Property CanWrite() As Boolean
> Get
> Return True
> End Get
> End Property
> Overrides ReadOnly Property Length() As Long
> Get
> Return 0
> End Get
> End Property
> Overrides Property Position() As Long
> Get
> Return _position
> End Get
> Set(ByVal Value As Long)
> _position = Value
> End Set
> End Property
> Public Overrides Function Seek(ByVal offset As
>Long, ByVal direction As SeekOrigin) As Long
> Return _sink.Seek(offset, direction)
> End Function
> Public Overrides Sub SetLength(ByVal length As
>Long)
> _sink.SetLength(length)
> End Sub
> Public Overrides Sub Close()
> _sink.Close()
> End Sub
> Public Overrides Sub Flush()
> _sink.Flush()
> End Sub
> Public Overloads Function Read(ByVal buffer() As
>Byte, ByVal offset As Int16, ByVal count As Int16) As Int16
> Return _sink.Read(buffer, offset, count)
> End Function
> Public Overloads Function Write(ByVal buffer() As
>Byte, ByVal offset As Int16, ByVal count As Int16)
> ReDim data(count)
> System.Buffer.BlockCopy(buffer, offset, data,
>0, count)
> _sink.Write(data, 0, count)
> End Function
> End Class
>but intelligence says to me that the class HTMLFilter must
>either be declared 'MustInherit' or override the following
>inherited 'MustOverride' members: Public Overridable
>MustOverride Overloads Sub Write(buffer() As Byte, offset
>As Integer, count As Integer), Public Overridable
>MustOverride Overloads Function Read(buffer() As Byte,
>offset As Integer, count As Integer) As Integer
>but if I understand i do it... please if you can help me...
>and if i understand the page is in de accesible punblic
>data that is a byte() , sorry if it is very simple but how
>i take it in a varible string , you say to instantiate the
>class
>Response.Filter= new HTMLFilter(Response.filter)
>but how i can take the HTML in string (sorry because is
>posible that i don't be a very good student) ;-)
>Xavi

0 comments:

Post a Comment