calling multiple web pages

  • Thread starter Thread starter Mark Fox
  • Start date Start date
M

Mark Fox

Hello,

I have a page DoBatchWork.aspx that my web host's
cron requests periodically. It currently calls another
web page that does some batch processing. The code I am
currently using in DoBatchWork.aspx is:

<%@ Page language="c#" EnableViewState="false"
ContentType="text/html" %>
<script language="C#" runat="server">

void Page_Load(Object Src, EventArgs E ) {

System.Net.HttpWebRequest HttpWReq =
(System.Net.HttpWebRequest) System.Net.WebRequest.Create
("http://www.mysite.com/sites/site1/DoSomeWork.aspx");
System.Net.HttpWebResponse HttpWResp =
(System.Net.HttpWebResponse)HttpWReq.GetResponse();

// Insert code that uses the response object.
// From "HttpWebResponse.GetResponseStream
Method" in documentation
System.IO.Stream receiveStream =
HttpWResp.GetResponseStream();
System.Text.Encoding encode =
System.Text.Encoding.GetEncoding("utf-8");
// Pipes the stream to a higher level stream reader
with the required encoding format.
System.IO.StreamReader readStream = new
System.IO.StreamReader( receiveStream, encode );
char[] read = new char[256];
// Reads 256 characters at a time.
int count = readStream.Read( read, 0, 256 );
while (count > 0)
{
// Dumps the 256 characters on a string
and displays the string to the console.
string str = new string(read, 0, count);
Response.Write(str);
count = readStream.Read(read, 0, 256);
}
// Releases the resources of the response.
HttpWResp.Close();
// Releases the resources of the Stream.
readStream.Close();


}

</script>

I got this code from the
HttpWebResponse.GetResponseStream method documentation.
It works great, but now I have a second page
DoMoreWork.aspx at a different URL
(http://www.mysite.com/site2/DoMoreWork.aspx) that I
would like DoBatchWork.aspx to also call in its page load
function. When I copied and pasted the code for calling
DoSomeWork.aspx (appending a "2" to all the variable
names), I get an error that asp.net is unable to read the
stream on line

System.IO.StreamReader readStream2 = new
System.IO.StreamReader( receiveStream2, encode2 );

I suspect this is because it seems the first page's
request closes the response object or something. My
question is this: How do I get it to call two pages?
Any help would appreciated. Thanks so much!
 
Hi Mark,

Thank you for posting to the MSDN newsgroups.

I am interested in this issue and researching on it now. I will update you
as soon as possible.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Mark,

I have done a lot of research regarding this issue. Unfortunately, I have
not found any hints now. Is it possible for you to write a simple testing
project only for this issue and tell me how to reproduce the problem on my
side step by step? I certainly appreciate your time.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Jacob,

Thank you for looking into this. I figured out how
to have the code call two pages instead of one, but maybe
you could help me figure out a secondary issue. The
following code calls the same page-www.solelsoftware.com--
twice in a row (although it could call two different
pages):

<%@ Page language="c#" EnableViewState="false"
ContentType="text/html" %>
<script language="C#" runat="server">

void Page_Load(Object Src, EventArgs E ) {

System.Net.HttpWebRequest HttpWReq =
(System.Net.HttpWebRequest) System.Net.WebRequest.Create
("http://www.solelsoftware.com/");
System.Net.HttpWebResponse HttpWResp =
(System.Net.HttpWebResponse)HttpWReq.GetResponse();

System.Net.HttpWebRequest HttpWReq2 =
(System.Net.HttpWebRequest) System.Net.WebRequest.Create
("http://www.solelsoftware.com/");
System.Net.HttpWebResponse HttpWResp2 =
(System.Net.HttpWebResponse)HttpWReq2.GetResponse();

// From "HttpWebResponse.GetResponseStream
Method" in documentation
System.IO.Stream receiveStream =
HttpWResp.GetResponseStream();
System.Text.Encoding encode =
System.Text.Encoding.GetEncoding("utf-8");
// Pipes the stream to a higher level stream reader
with the required encoding format.
System.IO.StreamReader readStream = new
System.IO.StreamReader( receiveStream, encode );
char[] read = new char[256];
// Reads 256 characters at a time.
int count = readStream.Read( read, 0, 256 );
while (count > 0)
{
// Dumps the 256 characters on a string
and displays the string to the console.
string str = new string(read, 0, count);
Response.Write(str);
count = readStream.Read(read, 0, 256);
}
// Releases the resources of the response.
HttpWResp.Close();
// Releases the resources of the Stream.
readStream.Close();


}

</script>

In the above code HttpWResp contains the response from
the first call and HttpWResp2 contains the response from
the second call. The code then goes on to read the
response from HttpWResp into a stream "receiveStream" and
write it out to the browser. How would I write the
responses from both HttpWResp and HttpWResp2 to the
browser? Would I append HttpWResp2 onto "receiveStream"
before outputting to the browser? Ideally I would like
to be able to insert some Response.Write statements in
between outputting HttpWResp and HttpWResp2, so that I
could put a divider/explanation before HttpWResp2's
output. Thank you for your help!
 
Hi Mark,

I apologize for it if there is any misunderstanding.

As I understand, what you really want to know is how to get the stream from
both HttpWResp and HttpWResp2. Please refer to the following C# console
sample carefully.
--------------------------------------------
using System;
using System.Net;
using System.IO;
using System.Text;

namespace TestRequest
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
// Creates an HttpWebRequest with the specified URL.
HttpWebRequest myHttpWebRequest =
(HttpWebRequest)WebRequest.Create("http://www.microsoft.com");
// Sends the HttpWebRequest and waits for the response.
HttpWebResponse myHttpWebResponse =
(HttpWebResponse)myHttpWebRequest.GetResponse();
// Gets the stream associated with the response.
Stream receiveStream = myHttpWebResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
// Pipes the stream to a higher level stream reader with the required
encoding format.
StreamReader readStream = new StreamReader( receiveStream, encode );
Console.WriteLine("\r\nResponse stream received.");
Char[] read = new Char[256];
// Reads 256 characters at a time.
int count = readStream.Read( read, 0, 256 );
Console.WriteLine("HTML...\r\n");
while (count > 0)
{
// Dumps the 256 characters on a string and displays the string to the
console.
String str = new String(read, 0, count);
Console.Write(str);
count = readStream.Read(read, 0, 256);
}
Console.WriteLine("");
// Releases the resources of the response.
myHttpWebResponse.Close();
// Releases the resources of the Stream.
readStream.Close();


HttpWebRequest myHttpWebRequest2 =
(HttpWebRequest)WebRequest.Create("http://www.microsoft.com");
// Sends the HttpWebRequest and waits for the response.
HttpWebResponse myHttpWebResponse2 =
(HttpWebResponse)myHttpWebRequest2.GetResponse();
// Gets the stream associated with the response.
Stream receiveStream2 = myHttpWebResponse2.GetResponseStream();
Encoding encode2 = System.Text.Encoding.GetEncoding("utf-8");
// Pipes the stream to a higher level stream reader with the required
encoding format.
StreamReader readStream2 = new StreamReader( receiveStream2, encode2 );
Console.WriteLine("\r\nResponse stream received.");
Char[] read2 = new Char[256];
// Reads 256 characters at a time.
int count2 = readStream2.Read( read2, 0, 256 );
Console.WriteLine("HTML...\r\n");
while (count2 > 0)
{
// Dumps the 256 characters on a string and displays the string to the
console.
String str = new String(read2, 0, count2);
Console.Write(str);
count2 = readStream2.Read(read2, 0, 256);
}
Console.WriteLine("");
// Releases the resources of the response.
myHttpWebResponse2.Close();
// Releases the resources of the Stream.
readStream2.Close();
}
}
}
--------------------------------------------

For the question of how to write the stream to the browser, I am not sure
about your exact meaning. Based on the above sample, what we get from the
stream is a HTML file that can be displayed/opened in IE. If you want to
write it to the browser, you need to parse the content of the stream
yourself to make it can be write to the browser.

I hope it helps.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Jacob,

Thank you for your response. This is what I was
looking for. I am sorry for not being clearer earlier.
Thanks again.

-----Original Message-----
Hi Mark,

I apologize for it if there is any misunderstanding.

As I understand, what you really want to know is how to get the stream from
both HttpWResp and HttpWResp2. Please refer to the following C# console
sample carefully.
--------------------------------------------
using System;
using System.Net;
using System.IO;
using System.Text;

namespace TestRequest
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
// Creates an HttpWebRequest with the specified URL.
HttpWebRequest myHttpWebRequest =
(HttpWebRequest)WebRequest.Create ("http://www.microsoft.com");
// Sends the HttpWebRequest and
waits for the response.
HttpWebResponse myHttpWebResponse
=
(HttpWebResponse)myHttpWebRequest.GetResponse();
// Gets the stream associated with the response.
Stream receiveStream = myHttpWebResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
// Pipes the stream to a higher
level stream reader with the required
encoding format.
StreamReader readStream = new
StreamReader( receiveStream, encode );
Console.WriteLine("\r\nResponse stream received.");
Char[] read = new Char[256];
// Reads 256 characters at a time.
int count = readStream.Read( read, 0, 256 );
Console.WriteLine("HTML...\r\n");
while (count > 0)
{
// Dumps the 256
characters on a string and displays the string to the
console.
String str = new String (read, 0, count);
Console.Write(str);
count = readStream.Read (read, 0, 256);
}
Console.WriteLine("");
// Releases the resources of the response.
myHttpWebResponse.Close();
// Releases the resources of the Stream.
readStream.Close();


HttpWebRequest myHttpWebRequest2
=
(HttpWebRequest)WebRequest.Create ("http://www.microsoft.com");
// Sends the HttpWebRequest and
waits for the response.
HttpWebResponse myHttpWebResponse2 =
(HttpWebResponse)myHttpWebRequest2.GetResponse();
// Gets the stream associated with the response.
Stream receiveStream2 = myHttpWebResponse2.GetResponseStream();
Encoding encode2 = System.Text.Encoding.GetEncoding("utf-8");
// Pipes the stream to a higher
level stream reader with the required
encoding format.
StreamReader readStream2 = new
StreamReader( receiveStream2, encode2 );
Console.WriteLine("\r\nResponse stream received.");
Char[] read2 = new Char[256];
// Reads 256 characters at a time.
int count2 = readStream2.Read( read2, 0, 256 );
Console.WriteLine("HTML...\r\n");
while (count2 > 0)
{
// Dumps the 256
characters on a string and displays the string to the
 
Back
Top