RE: How to read content of httpResponse.OutputStream

G

Guest

Please i'd like an answer to my question
I have an ASP.NET Web Service and for demosntration purposes i wan't to know the structure of the response that is sent to client
I don't want to use SoapExtensions i need to do that int the EndRequest event

Thank you
i'll wait
 
A

Alvin Bruney [MVP]

what do you mean structure? The structure is the type which is explictly an
IO stream. Do you want to read the stream?

StreamReader sr = new StreamReader(put your response object stream,
System.Text.Encoding.UTF8);
string pageContent = sr.ReadToEnd();

roughly
 
G

Guest

yes i want to read from the strea
sorry, i don't speak english wel

i liked your answer and i've tried but i have this error: Stream was not readable. (at line 2

This is the code i have at EndRequest even

1 Dim httpApp As HttpApplication = CType(sender, HttpApplication
2 Dim sr As New System.IO.StreamReader(httpApp.Response.OutputStream, System.Text.Encoding.UTF8
3 Dim contenido As String = sr.ReadToEn
4 Debug.WriteLine(DateTime.Now.ToString
5 Debug.WriteLine(contenido
6 Debug.Flush(

Thank you Alvin, i m trying other ways but i still can't
The purpose is to read the Response.OutputStream
 
A

Alvin Bruney [MVP]

why do you need to read the output stream? If you want to do something like
a screen scrape or read the results of an html page as an example, it's
easier to use the webrequest class. let me know, i haven't tried your code
yet
 
G

Guest

I'm preparing a course (don't smile please) and i want to explain my students the diferences between invoke a web service via HTTP GET, HTTP POST and SOAP. So i've prepared made this code to intercept the request from clients and to explain these differences(please ignore security and performance now). (I only write Request.InputStream to a file

<%@ Application %><script language="vb" runat="server"
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs
Dim sNombre As Strin
'sNombre = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache
sNombre = Server.MapPath(Request.ApplicationPath
sNombre &= "\peticion" & DateTime.Now.Hour.ToString("00") & DateTime.Now.Minute.ToString("00") & DateTime.Now.Second.ToString("00") & DateTime.Now.Millisecond.ToString("0000") & ".txt
Dim fs As System.IO.FileStream = System.IO.File.Create(sNombre
Dim sw As System.IO.StreamWriter = New System.IO.StreamWriter(fs
sw.Write("URL: " & Request.Url.ToString & System.Environment.NewLine
sw.Write("Tamaño en Bytes: " & Request.ContentLength.ToString & Environment.NewLine
If Request.ContentLength <> 0 The
Dim contenido(Request.ContentLength) As Byt
Dim sContenido As Strin
Request.InputStream.Read(contenido, 0, Request.InputStream.Length
Request.InputStream.Position =
Dim i As Intege
For i = 0 To Request.ContentLength -
sContenido &= System.Convert.ToChar(contenido(i)
Nex
sw.Write("Contenido:" & Environment.NewLine
sw.Write(sContenido
End I
sw.Flush(
sw.Close(
End Su
</script

With the code above i can intercept a request and analyze that to show my students the exact differences
Now i wan't to show them the response that is sent to clien
I know response is the same no matter what i've used to call web service(http get, http post or soap), but i wan't to show my students and say them hey here is the response not using the explorer, i want to show the response via code
Thank you
 
G

Guest

I will try using WebRequest and WebResponse classes this weekend
Thanks for your ideas, if you have a more simple way of analyze the Request.InputStream and Response.Outputstream please tell me
Thank you
Saludos desde Perú
 
A

Alvin Bruney [MVP]

You're in luck!
It turns out that I wrote some rough code yesterday for a side project. You
should be able to modify it to get what you want.
The code basically fires a 10 digit telephone number along with a url
against the website and intercepts the response stream to find the address
matching the phone number. So you can replace the uri with your own uri and
skip the if statement part. I think I got the original code from Peter
Bromberg's reverse ani lookup.

// Create a new 'Uri' object with the specified string.
Uri myUri =new
Uri("http://www.anywho.com/qry/wp_rl?npa="+strNumber.Substring(0,3) +
"&telephone="+ strNumber.Substring(3,7) + "&btnsubmit.x=36&btnsubmit.y=9");
// Creates an HttpWebRequest with the specified URL.
HttpWebRequest myHttpWebRequest =
(HttpWebRequest)WebRequest.Create(myUri);
myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows
NT 5.1; Q312461; .NET CLR 1.0.3705)";
HttpWebResponse res = (HttpWebResponse)myHttpWebRequest.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream(),
System.Text.Encoding.UTF8);
string pageContent = sr.ReadToEnd();
res.Close();
sr.Close();

//parse the response looking for the goodies
int startpos = pageContent.IndexOf(@"bin/amap.cgi?") + 10;
if(startpos != -1)
{
int endpost = pageContent.IndexOf("Maps &amp; Directions");
if(endpost != -1)
searchtext = pageContent.Substring(startpos, endpost - startpos + 17);

strResult = searchtext;
sr.Close();
}
 
G

Guest

Thank you Alvin.
I'm working now with httpWebRequest and httpWebResponse and they work the way i want.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top