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


I have a URL in a string and there is a file at that URL lets say
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);


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

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I would use a streamreader and a streamwriter.
The streamwriter can write into the bytearray instead of a file have a look at this example


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Carlos VillegasFull Stack .NET DeveloperCommented:
int byteCount = (int)WebResp.ContentLength;

Open in new window

Carlos VillegasFull Stack .NET DeveloperCommented:
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

Carlos VillegasFull Stack .NET DeveloperCommented:
Also I recommend you to explore the System.Net.WebClient class, it is very useful for what you want to do, and simple.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.