Link to home
Start Free TrialLog in
Avatar of at999
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

thanks

Avatar of at999
at999

ASKER

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...
Avatar of at999

ASKER

>>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.
Avatar of at999

ASKER

So should i rely on such stream to display images in asp.net? thanks... Do you have any other idea? thanks
SOLUTION
Avatar of SunnyDark
SunnyDark
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of at999

ASKER

>> 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???  
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of at999

ASKER

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

ASKER

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