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

Posted on 2011-10-04
Medium Priority
Last Modified: 2012-05-12

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


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

Question by:dev_intagleo
  • 3

Accepted Solution

karl-henrik earned 2000 total points
ID: 36910753
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

LVL 17

Expert Comment

by:Carlos Villegas
ID: 36910776
int byteCount = (int)WebResp.ContentLength;

Open in new window

LVL 17

Expert Comment

by:Carlos Villegas
ID: 36910803
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

LVL 17

Expert Comment

by:Carlos Villegas
ID: 36910827
Also I recommend you to explore the System.Net.WebClient class, it is very useful for what you want to do, and simple.


Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month16 days, 23 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question