unable to download file using HttpWebRequest in c#

mmalik15
mmalik15 used Ask the Experts™
on
using a webservice i am trying to download files (*.doc, *.rtf , *.txt etc) but getting the following exception


.GetResponseStream().Length' threw an exception of type 'System.NotSupportedException' long {System.NotSupportedException}

'httpResponse.GetResponseStream().Position' threw an exception of type 'System.NotSupportedException' long {System.NotSupportedException}

{"This stream does not support seek operations."} System.SystemException {System.NotSupportedException}

the code i m using is
HttpWebRequest fileReq = (HttpWebRequest)HttpWebRequest.Create("http://member.goodpractice.net/content.htm" + queryString);

            //Create a response for this request
            HttpWebResponse fileResp = (HttpWebResponse)fileReq.GetResponse();

            if (fileResp.ContentLength > 0)
            {
                //fileResp.ContentLength = fileResp.ContentLength;

                //Get the Stream returned from the response
                Stream stream = fileResp.GetResponseStream();
            }

Open in new window


The absolute url e.g. (http://member.goodpractice.net/content.htm?GWSUsername=xxx&GWSPassword=xxx&format=4&id=582805c7-6386-0029-b068-9fe900b4c257) works perfectly fine when opened in a browser it would download the file  without any issues. Any ideas what could be wrong with this code
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Lead Software Engineer
Commented:
In order to save files to the machine you need to read the data from the response and write it to an file.

Another much easier way is to use the WebClient

WebClient client = new WebClient();
client.DownloadFile(sourceUrl, fileName);

Open in new window


The problem only is with WebClient that it cannot set an timeout and more advanced things.

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