iqula
asked on
System.Net.HttpWebRequest.Create(url), something simular.
Ok, what I which to acheive is to send my page to an email everytime a person views it.
The emailing part is ok, but how can I save the response.outputstream of my aspx page into a string, once it has run?
Yea it's an odd question I know.
I have this code, which does this from another page but I want to get the stuff from the page that is running.
private String readHtmlPage(string url)
{
String result;
System.Net.WebResponse objResponse;
System.Net.WebRequest objRequest = System.Net.HttpWebRequest. Create(url );
objResponse = objRequest.GetResponse();
using (System.IO.StreamReader sr = new System.IO.StreamReader(obj Response.G etResponse Stream()) )
{
result = sr.ReadToEnd();
// Close and clean up the StreamReader
sr.Close();
}
return result;
}
The emailing part is ok, but how can I save the response.outputstream of my aspx page into a string, once it has run?
Yea it's an odd question I know.
I have this code, which does this from another page but I want to get the stuff from the page that is running.
private String readHtmlPage(string url)
{
String result;
System.Net.WebResponse objResponse;
System.Net.WebRequest objRequest = System.Net.HttpWebRequest.
objResponse = objRequest.GetResponse();
using (System.IO.StreamReader sr = new System.IO.StreamReader(obj
{
result = sr.ReadToEnd();
// Close and clean up the StreamReader
sr.Close();
}
return result;
}
ASKER
Thanks, but this appends to the output stream, what I need is the output stream as a string, or am I missing something here :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This looks very promising, how can I get the string instead of writinf to a file?
you can use this code in overriden void Close()
sr = new StreamReader(@"C:\FilterOu tput\respo nse.htm");
s = sr.ReadToEnd();
sr.Close();
and after that to send s to static variable in your class
so every time you submit a page at the end of the request you will send the content of this file and when you enter again in the PageLoad you can get it
B..G
sr = new StreamReader(@"C:\FilterOu
s = sr.ReadToEnd();
sr.Close();
and after that to send s to static variable in your class
so every time you submit a page at the end of the request you will send the content of this file and when you enter again in the PageLoad you can get it
B..G
ASKER
I'm not sure how you chaps know this stuff, but I'm really impressed :)
Here is my solution: Thanks
public class EmailThisPage :Stream
{
//Call from an aspx page within the OnInit(EventArgs e), like this
//------------------------ ---------- ---------- ---------- ---------- --
//tools.EmailThisPage ep = new tools.EmailThisPage(Respon se.Filter) ;
//ep.From="name@domain.com ";
//ep.To="name@domain.com";
//ep.Subject="A page email";
//ep.SmtpServer="localhost ";
//Response.Filter = ep;
//------------------------ ---------- ---------- ---------- ---------- --
private Stream m_sink;
private long m_position;
public string From, To, Cc, Bcc, Subject, SmtpServer;
public Exception error;
public EmailThisPage(Stream sink)//Response.Filter
{
m_sink = sink;
}
// The following members of Stream must be overriden.
public override bool CanRead
{get { return true; }}
public override bool CanSeek
{get { return false; }}
public override bool CanWrite
{get { return false; }}
public override long Length
{get { return 0; }}
public override long Position
{
get { return m_position; }
set { m_position = value; }
}
public override long Seek(long offset, System.IO.SeekOrigin direction)
{
return 0;
}
public override void SetLength(long length)
{
m_sink.SetLength(length);
}
public override void Close()
{
m_sink.Close();
}
public override void Flush()
{
m_sink.Flush();
}
public override int Read(byte[] buffer, int offset, int count)
{
return m_sink.Read(buffer, offset, count);
}
// Override the Write method to filter Response to a file.
public override void Write(byte[] buffer, int offset, int count)
{
string body;
//Write out the response to the browser.
m_sink.Write(buffer, 0, count);
body=System.Convert.ToBase 64String(b uffer, 0, buffer.Length);
System.Text.UTF8Encoding objUTF8 =new System.Text.UTF8Encoding() ;
body= objUTF8.GetString(Convert. FromBase64 String(bod y));
//Write out the response to the string.
try
{
MailMessage Mailer = new MailMessage();
SmtpMail.SmtpServer = SmtpServer;
Mailer.From = From;
Mailer.To = To;
Mailer.Cc = Cc;
Mailer.Bcc = Bcc;
Mailer.Subject = Subject;
Mailer.Body =body;
Mailer.BodyFormat = System.Web.Mail.MailFormat .Html;
SmtpMail.Send(Mailer);
}
catch
{
MailMessage Mailer = new MailMessage();
SmtpMail.SmtpServer = SmtpServer;
Mailer.From = From;
Mailer.Cc = Cc;
Mailer.Bcc = Bcc;
Mailer.Body =body;
Mailer.BodyFormat = System.Web.Mail.MailFormat .Html;
Mailer.To = System.Configuration.Confi gurationSe ttings.App Settings[" erroremail "];
Mailer.Subject = "Email Error (To:)" + To + " (Subject:)" + Subject;
SmtpMail.Send(Mailer);
}
}
}
Here is my solution: Thanks
public class EmailThisPage :Stream
{
//Call from an aspx page within the OnInit(EventArgs e), like this
//------------------------
//tools.EmailThisPage ep = new tools.EmailThisPage(Respon
//ep.From="name@domain.com
//ep.To="name@domain.com";
//ep.Subject="A page email";
//ep.SmtpServer="localhost
//Response.Filter = ep;
//------------------------
private Stream m_sink;
private long m_position;
public string From, To, Cc, Bcc, Subject, SmtpServer;
public Exception error;
public EmailThisPage(Stream sink)//Response.Filter
{
m_sink = sink;
}
// The following members of Stream must be overriden.
public override bool CanRead
{get { return true; }}
public override bool CanSeek
{get { return false; }}
public override bool CanWrite
{get { return false; }}
public override long Length
{get { return 0; }}
public override long Position
{
get { return m_position; }
set { m_position = value; }
}
public override long Seek(long offset, System.IO.SeekOrigin direction)
{
return 0;
}
public override void SetLength(long length)
{
m_sink.SetLength(length);
}
public override void Close()
{
m_sink.Close();
}
public override void Flush()
{
m_sink.Flush();
}
public override int Read(byte[] buffer, int offset, int count)
{
return m_sink.Read(buffer, offset, count);
}
// Override the Write method to filter Response to a file.
public override void Write(byte[] buffer, int offset, int count)
{
string body;
//Write out the response to the browser.
m_sink.Write(buffer, 0, count);
body=System.Convert.ToBase
System.Text.UTF8Encoding objUTF8 =new System.Text.UTF8Encoding()
body= objUTF8.GetString(Convert.
//Write out the response to the string.
try
{
MailMessage Mailer = new MailMessage();
SmtpMail.SmtpServer = SmtpServer;
Mailer.From = From;
Mailer.To = To;
Mailer.Cc = Cc;
Mailer.Bcc = Bcc;
Mailer.Subject = Subject;
Mailer.Body =body;
Mailer.BodyFormat = System.Web.Mail.MailFormat
SmtpMail.Send(Mailer);
}
catch
{
MailMessage Mailer = new MailMessage();
SmtpMail.SmtpServer = SmtpServer;
Mailer.From = From;
Mailer.Cc = Cc;
Mailer.Bcc = Bcc;
Mailer.Body =body;
Mailer.BodyFormat = System.Web.Mail.MailFormat
Mailer.To = System.Configuration.Confi
Mailer.Subject = "Email Error (To:)" + To + " (Subject:)" + Subject;
SmtpMail.Send(Mailer);
}
}
}
look the example
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconcustomhttpmodules.asp?frame=true
B..G