ASP .NET HttpWebRequest.GetResponseStream()

at999
at999 used Ask the Experts™
on
I have a servlet page- http://mylink/servletpage which reads a JPEG image file and writes bytes to a servletoutputstream... So when i open the link i can actually see that image file on the browser.

Now i want to read that Output stream using .NET...

So i wrote this code -
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(" http://mylink/servletpage");
                     
        HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
        Response.Write(" myHttpWebResponse.ContentType " + myHttpWebResponse.ContentType) ; //THIS CONTENT TYPE IS ALWAYS JPEG
        // Gets the stream associated with the response.
        Stream receiveStream = myHttpWebResponse.GetResponseStream();

Now i want that response-stream back to a JPEG file...

Pl help

thanks

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
I tried using receiveStream.ReadByte();   and then looping throug the receiveStream and writitng each byte to the jpeg file -

        System.IO.FileStream fs;
        fs = new FileStream("c:\\myfile.jpg", FileMode.CreateNew);

        fs.WriteByte() ; //till readbyte returns -1....

 but that loop never ended, it created a file of size 3 gig.... and still it was going on  
Use HttpWebResponse.ContentLength to determine the file size...

Byte[] data = new Byte[myHttpWebResponse.ContentLength];
receiveStream.Read(data, 0,myHttpWebResponse.ContentLength);
fs.Write(data);
Also it's important to .Flush end .Close the servlet output stream...
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Author

Commented:
>>Byte[] data = new Byte[myHttpWebResponse.ContentLength];
contentlength is -1 Here


i donot know why so its failing thanks
Then the problem is on the server side (in the servlet) see my second comment , this is the main reason for such errors.
The fact that you can see the JPEG by pointing the browser to the servlet url alone is not guarantying that the data is transmitted correctly.

Author

Commented:
So should i rely on such stream to display images in asp.net? thanks... Do you have any other idea? thanks
If you have a control over the servlet I would suggest fixing it...
Otherwise you may try :
Bitmap bmp = new Bitmap(myHttpWebResponse.GetResponseStream());
bmp.Save("C:\\my file.jpg");

Author

Commented:
>> If you have a control over the servlet I would suggest fixing it...
Ya i have the other java developer its his servlet code and he can change it to make it work...
But what should be fixed here???  
I am not sure how it is done in java servlet, however if I want to send out a jpeg file from ASP or PHP I read the file , write it our to the response stream than call .Flush and .End on the stream.Perhaps he also might manually set the ContentLength header on the response stream

Author

Commented:
OK thanks thats making it clearer.... i am going to try it

Author

Commented:
I wrote bytes to a memorystream and it worked.... thanks a lot

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial