webrequest help

Discussion in 'ASP .Net' started by rodchar, Oct 14, 2008.

  1. rodchar

    rodchar Guest

    hi all,

    given:
    HttpWebRequest WebReq =
    (HttpWebRequest)WebRequest.Create("myurl");
    WebReq.Method = "GET";
    HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
    Response.Write(WebResp.StatusCode);
    Response.Write(WebResp.Server);

    Stream Answer = WebResp.GetResponseStream();
    StreamReader _Answer = new StreamReader(Answer);

    this url is retrieving a .pdf file. how do i get my pdf file to display on
    my web page?

    thanks,
    rodchar
    rodchar, Oct 14, 2008
    #1
    1. Advertising

  2. When you stream directly to the stream, you have to set the MIME type in the
    page before the code that starts the stream. Once you have set the type, the
    browser should recongnize the file as whatever type you have told the
    browser that file is.

    If that is overbearing, you can stream the file to a folder in the website
    and redirect the user to that file. Just make sure you have a mechanism that
    cleans up old files.

    --
    Gregory A. Beamer
    MVP, MCP: +I, SE, SD, DBA

    Subscribe to my blog
    http://feeds.feedburner.com/GregoryBeamer#

    or just read it:
    http://feeds.feedburner.com/GregoryBeamer

    ********************************************
    | Think outside the box! |
    ********************************************
    "rodchar" <> wrote in message
    news:...
    > hi all,
    >
    > given:
    > HttpWebRequest WebReq =
    > (HttpWebRequest)WebRequest.Create("myurl");
    > WebReq.Method = "GET";
    > HttpWebResponse WebResp =
    > (HttpWebResponse)WebReq.GetResponse();
    > Response.Write(WebResp.StatusCode);
    > Response.Write(WebResp.Server);
    >
    > Stream Answer = WebResp.GetResponseStream();
    > StreamReader _Answer = new StreamReader(Answer);
    >
    > this url is retrieving a .pdf file. how do i get my pdf file to display on
    > my web page?
    >
    > thanks,
    > rodchar
    Cowboy \(Gregory A. Beamer\), Oct 14, 2008
    #2
    1. Advertising

  3. rodchar

    rodchar Guest

    once i set the MIME type, how, then, do i take the stream and display it my
    web page?

    "Cowboy (Gregory A. Beamer)" wrote:

    > When you stream directly to the stream, you have to set the MIME type in the
    > page before the code that starts the stream. Once you have set the type, the
    > browser should recongnize the file as whatever type you have told the
    > browser that file is.
    >
    > If that is overbearing, you can stream the file to a folder in the website
    > and redirect the user to that file. Just make sure you have a mechanism that
    > cleans up old files.
    >
    > --
    > Gregory A. Beamer
    > MVP, MCP: +I, SE, SD, DBA
    >
    > Subscribe to my blog
    > http://feeds.feedburner.com/GregoryBeamer#
    >
    > or just read it:
    > http://feeds.feedburner.com/GregoryBeamer
    >
    > ********************************************
    > | Think outside the box! |
    > ********************************************
    > "rodchar" <> wrote in message
    > news:...
    > > hi all,
    > >
    > > given:
    > > HttpWebRequest WebReq =
    > > (HttpWebRequest)WebRequest.Create("myurl");
    > > WebReq.Method = "GET";
    > > HttpWebResponse WebResp =
    > > (HttpWebResponse)WebReq.GetResponse();
    > > Response.Write(WebResp.StatusCode);
    > > Response.Write(WebResp.Server);
    > >
    > > Stream Answer = WebResp.GetResponseStream();
    > > StreamReader _Answer = new StreamReader(Answer);
    > >
    > > this url is retrieving a .pdf file. how do i get my pdf file to display on
    > > my web page?
    > >
    > > thanks,
    > > rodchar

    >
    >
    rodchar, Oct 15, 2008
    #3
  4. rodchar

    Ben Amada Guest

    rodchar wrote:

    > once i set the MIME type, how, then, do i take the stream and display it
    > my web page?


    You read the stream into a byte array, and then output the byte array into
    the Response object. For a previous project, I found that adding a
    content-disposition header helped (for Safari, I think).

    .... your previous code ...
    Stream Answer = WebResp.GetResponseStream();

    int PdfByteCount = (int)Answer.Length - 1;
    byte[] pdfBytes = new byte[PdfByteCount];
    Answer.Read(pdfBytes, 0, PdfByteCount);

    Response.ClearContent();
    Response.ClearHeaders();
    Response.AddHeader(
    "Content-disposition",
    "inline; filename=some_file_name.pdf");
    Response.ContentType = "application/pdf";
    Response.BinaryWrite(pdfBytes);
    Response.End();

    --
    Ben
    http://allben.net/
    Ben Amada, Oct 15, 2008
    #4
  5. rodchar

    Ben Amada Guest

    Ben Amada wrote:

    > int PdfByteCount = (int)Answer.Length - 1;
    > byte[] pdfBytes = new byte[PdfByteCount];
    > Answer.Read(pdfBytes, 0, PdfByteCount);


    That's not correct, should be:

    byte[] pdfBytes = new byte[Answer.Length];
    Answer.Read(pdfBytes, 0, (int)Answer.Length);
    Ben Amada, Oct 15, 2008
    #5
  6. rodchar

    bruce barker Guest

    a pdf is webrequest response. it will replace the page with the pdf.
    normally, you would have a link the user hits that returns the pdf file.
    if you want it inline in the page, then you add an iframe to page, and
    set the resource the pdf url.

    -- bruce (sqlwork.com)

    rodchar wrote:
    > once i set the MIME type, how, then, do i take the stream and display it my
    > web page?
    >
    > "Cowboy (Gregory A. Beamer)" wrote:
    >
    >> When you stream directly to the stream, you have to set the MIME type in the
    >> page before the code that starts the stream. Once you have set the type, the
    >> browser should recongnize the file as whatever type you have told the
    >> browser that file is.
    >>
    >> If that is overbearing, you can stream the file to a folder in the website
    >> and redirect the user to that file. Just make sure you have a mechanism that
    >> cleans up old files.
    >>
    >> --
    >> Gregory A. Beamer
    >> MVP, MCP: +I, SE, SD, DBA
    >>
    >> Subscribe to my blog
    >> http://feeds.feedburner.com/GregoryBeamer#
    >>
    >> or just read it:
    >> http://feeds.feedburner.com/GregoryBeamer
    >>
    >> ********************************************
    >> | Think outside the box! |
    >> ********************************************
    >> "rodchar" <> wrote in message
    >> news:...
    >>> hi all,
    >>>
    >>> given:
    >>> HttpWebRequest WebReq =
    >>> (HttpWebRequest)WebRequest.Create("myurl");
    >>> WebReq.Method = "GET";
    >>> HttpWebResponse WebResp =
    >>> (HttpWebResponse)WebReq.GetResponse();
    >>> Response.Write(WebResp.StatusCode);
    >>> Response.Write(WebResp.Server);
    >>>
    >>> Stream Answer = WebResp.GetResponseStream();
    >>> StreamReader _Answer = new StreamReader(Answer);
    >>>
    >>> this url is retrieving a .pdf file. how do i get my pdf file to display on
    >>> my web page?
    >>>
    >>> thanks,
    >>> rodchar

    >>
    bruce barker, Oct 15, 2008
    #6
  7. rodchar

    rodchar Guest

    i'm getting message:
    This stream does not support seek operations.

    Here's some of what i'm doing at the source of the url:

    context.Response.Clear();
    context.Response.ClearHeaders();
    context.Response.ClearContent();
    context.Response.ContentType = @"application/pdf";
    //context.Response.AddHeader("content-disposition",
    String.Format("inline; filename={0}.pdf", adNumber));
    context.Response.AddHeader("content-disposition",
    String.Format("attachment; filename={0}.pdf", adNumber));

    byte[] image = new byte[fileInfo.Length];
    using (BinaryReader reader = new BinaryReader(new
    FileStream(path, FileMode.Open, FileAccess.Read)))
    {
    image = reader.ReadBytes((int)fileInfo.Length);
    }

    context.Response.BinaryWrite(image);
    context.Response.Flush();
    context.Response.Close();
    context.Response.End();


    "bruce barker" wrote:

    > a pdf is webrequest response. it will replace the page with the pdf.
    > normally, you would have a link the user hits that returns the pdf file.
    > if you want it inline in the page, then you add an iframe to page, and
    > set the resource the pdf url.
    >
    > -- bruce (sqlwork.com)
    >
    > rodchar wrote:
    > > once i set the MIME type, how, then, do i take the stream and display it my
    > > web page?
    > >
    > > "Cowboy (Gregory A. Beamer)" wrote:
    > >
    > >> When you stream directly to the stream, you have to set the MIME type in the
    > >> page before the code that starts the stream. Once you have set the type, the
    > >> browser should recongnize the file as whatever type you have told the
    > >> browser that file is.
    > >>
    > >> If that is overbearing, you can stream the file to a folder in the website
    > >> and redirect the user to that file. Just make sure you have a mechanism that
    > >> cleans up old files.
    > >>
    > >> --
    > >> Gregory A. Beamer
    > >> MVP, MCP: +I, SE, SD, DBA
    > >>
    > >> Subscribe to my blog
    > >> http://feeds.feedburner.com/GregoryBeamer#
    > >>
    > >> or just read it:
    > >> http://feeds.feedburner.com/GregoryBeamer
    > >>
    > >> ********************************************
    > >> | Think outside the box! |
    > >> ********************************************
    > >> "rodchar" <> wrote in message
    > >> news:...
    > >>> hi all,
    > >>>
    > >>> given:
    > >>> HttpWebRequest WebReq =
    > >>> (HttpWebRequest)WebRequest.Create("myurl");
    > >>> WebReq.Method = "GET";
    > >>> HttpWebResponse WebResp =
    > >>> (HttpWebResponse)WebReq.GetResponse();
    > >>> Response.Write(WebResp.StatusCode);
    > >>> Response.Write(WebResp.Server);
    > >>>
    > >>> Stream Answer = WebResp.GetResponseStream();
    > >>> StreamReader _Answer = new StreamReader(Answer);
    > >>>
    > >>> this url is retrieving a .pdf file. how do i get my pdf file to display on
    > >>> my web page?
    > >>>
    > >>> thanks,
    > >>> rodchar
    > >>

    >
    rodchar, Oct 15, 2008
    #7
  8. rodchar

    rodchar Guest

    thanks everyone for the help,
    rod.

    "rodchar" wrote:

    > hi all,
    >
    > given:
    > HttpWebRequest WebReq =
    > (HttpWebRequest)WebRequest.Create("myurl");
    > WebReq.Method = "GET";
    > HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
    > Response.Write(WebResp.StatusCode);
    > Response.Write(WebResp.Server);
    >
    > Stream Answer = WebResp.GetResponseStream();
    > StreamReader _Answer = new StreamReader(Answer);
    >
    > this url is retrieving a .pdf file. how do i get my pdf file to display on
    > my web page?
    >
    > thanks,
    > rodchar
    rodchar, Oct 16, 2008
    #8
  9. You can move data from a stream to another stream. Outputting as bytes is
    another option, per Ben's post (have not gone through his code yet to ensure
    it is correct).

    --
    Gregory A. Beamer
    MVP, MCP: +I, SE, SD, DBA

    Subscribe to my blog
    http://feeds.feedburner.com/GregoryBeamer#

    or just read it:
    http://feeds.feedburner.com/GregoryBeamer

    ********************************************
    | Think outside the box! |
    ********************************************
    "rodchar" <> wrote in message
    news:...
    > i'm getting message:
    > This stream does not support seek operations.
    >
    > Here's some of what i'm doing at the source of the url:
    >
    > context.Response.Clear();
    > context.Response.ClearHeaders();
    > context.Response.ClearContent();
    > context.Response.ContentType = @"application/pdf";
    > //context.Response.AddHeader("content-disposition",
    > String.Format("inline; filename={0}.pdf", adNumber));
    > context.Response.AddHeader("content-disposition",
    > String.Format("attachment; filename={0}.pdf", adNumber));
    >
    > byte[] image = new byte[fileInfo.Length];
    > using (BinaryReader reader = new BinaryReader(new
    > FileStream(path, FileMode.Open, FileAccess.Read)))
    > {
    > image = reader.ReadBytes((int)fileInfo.Length);
    > }
    >
    > context.Response.BinaryWrite(image);
    > context.Response.Flush();
    > context.Response.Close();
    > context.Response.End();
    >
    >
    > "bruce barker" wrote:
    >
    >> a pdf is webrequest response. it will replace the page with the pdf.
    >> normally, you would have a link the user hits that returns the pdf file.
    >> if you want it inline in the page, then you add an iframe to page, and
    >> set the resource the pdf url.
    >>
    >> -- bruce (sqlwork.com)
    >>
    >> rodchar wrote:
    >> > once i set the MIME type, how, then, do i take the stream and display
    >> > it my
    >> > web page?
    >> >
    >> > "Cowboy (Gregory A. Beamer)" wrote:
    >> >
    >> >> When you stream directly to the stream, you have to set the MIME type
    >> >> in the
    >> >> page before the code that starts the stream. Once you have set the
    >> >> type, the
    >> >> browser should recongnize the file as whatever type you have told the
    >> >> browser that file is.
    >> >>
    >> >> If that is overbearing, you can stream the file to a folder in the
    >> >> website
    >> >> and redirect the user to that file. Just make sure you have a
    >> >> mechanism that
    >> >> cleans up old files.
    >> >>
    >> >> --
    >> >> Gregory A. Beamer
    >> >> MVP, MCP: +I, SE, SD, DBA
    >> >>
    >> >> Subscribe to my blog
    >> >> http://feeds.feedburner.com/GregoryBeamer#
    >> >>
    >> >> or just read it:
    >> >> http://feeds.feedburner.com/GregoryBeamer
    >> >>
    >> >> ********************************************
    >> >> | Think outside the box! |
    >> >> ********************************************
    >> >> "rodchar" <> wrote in message
    >> >> news:...
    >> >>> hi all,
    >> >>>
    >> >>> given:
    >> >>> HttpWebRequest WebReq =
    >> >>> (HttpWebRequest)WebRequest.Create("myurl");
    >> >>> WebReq.Method = "GET";
    >> >>> HttpWebResponse WebResp =
    >> >>> (HttpWebResponse)WebReq.GetResponse();
    >> >>> Response.Write(WebResp.StatusCode);
    >> >>> Response.Write(WebResp.Server);
    >> >>>
    >> >>> Stream Answer = WebResp.GetResponseStream();
    >> >>> StreamReader _Answer = new StreamReader(Answer);
    >> >>>
    >> >>> this url is retrieving a .pdf file. how do i get my pdf file to
    >> >>> display on
    >> >>> my web page?
    >> >>>
    >> >>> thanks,
    >> >>> rodchar
    >> >>

    >>
    Cowboy \(Gregory A. Beamer\), Oct 16, 2008
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. CodeMotion
    Replies:
    0
    Views:
    546
    CodeMotion
    Jul 3, 2003
  2. Andres
    Replies:
    2
    Views:
    6,619
    Natty Gur
    Aug 14, 2003
  3. Sujith Jagini

    problem with webRequest.method = post

    Sujith Jagini, Aug 16, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    1,468
    Natty Gur
    Aug 17, 2003
  4. John Saunders

    Re: problem with webRequest.method = post

    John Saunders, Aug 20, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    406
    John Saunders
    Aug 20, 2003
  5. =?Utf-8?B?TmFnYQ==?=
    Replies:
    0
    Views:
    671
    =?Utf-8?B?TmFnYQ==?=
    Jun 14, 2006
Loading...

Share This Page