Avatar of at999
 asked on

ASP .NET HttpWebRequest.GetResponseStream()

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



Avatar of undefined
Last Comment

8/22/2022 - Mon

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);

Also it's important to .Flush end .Close the servlet output stream...
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy

>>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.

So should i rely on such stream to display images in asp.net? thanks... Do you have any other idea? thanks
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

>> 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???  
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.

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

I wrote bytes to a memorystream and it worked.... thanks a lot
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes