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

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

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

Question by:dev_intagleo
    LVL 3

    Accepted Solution

    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
    int byteCount = (int)WebResp.ContentLength;

    Open in new window

    LVL 17

    Expert Comment

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

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    730 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now