Link to home
Start Free TrialLog in
Avatar of dev_intagleo
dev_intagleo

asked on

create a stream from a URL and then write that stream to response

Hi,

I have a URL in a string and there is a file at that URL lets say abc.com/file.js
Now, i want to load that file in a stream (i think by byte array) and then reponse.write on the page

so that file.js or any other text file appeared on the page.

So far my code is
path variable contains the url
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(path);
            WebReq.Method = "GET";
            HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
            Stream str = WebResp.GetResponseStream();

            int byteCount = (int)str.Length;
            byte[] fileBytes = new byte[byteCount];
            str.Read(fileBytes, 0, byteCount);

            Response.ClearContent();
            Response.ClearHeaders();
            Response.BinaryWrite(fileBytes);
            Response.End();

Open in new window




currently its giving me error

"System.NotSupportedException: This stream does not support seek operations."

on Line

int byteCount = (int)str.Length;

Open in new window




ASKER CERTIFIED SOLUTION
Avatar of karl-henrik
karl-henrik
Flag of Sweden 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
Try:
int byteCount = (int)WebResp.ContentLength;

Open in new window

And if you want to write to the server then you must to use the post method in a separate request.
WebReq.Method = "POST";

Open in new window

Also I recommend you to explore the System.Net.WebClient class, it is very useful for what you want to do, and simple.

Reference:
http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx