Download file using c# client

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Vasu

    Download file using c# client

    Hi,

    I have a requirement to download a file from the web
    site using a client tool.

    Iam writing a C# program to download using WebRequest,
    HttpRequest, WebResponse and

    so on. The problem Iam having is to navigate thru
    multiple pages. I have to login

    to the web site, choose an option from the list box and
    then click on the download

    button. Iam able to login to the site, while choosing the
    selections from the drop

    down and posting to the site it is lossing its connection
    state. In short Iam not

    able to use the same WebRequest object to post the next
    URL. The web server is

    holding a session with my connection and Iam not able to
    go to the next page using

    the same object, I have to create a new object with the
    next URL which would loose

    the session state. Any help would be appreciated.


    Thanks
    Vasu
  • Joerg Jooss

    #2
    Re: Download file using c# client

    "Vasu" wrote:
    [color=blue]
    > Hi,
    >
    > I have a requirement to download a file from the web
    > site using a client tool.
    >
    > Iam writing a C# program to download using WebRequest,
    > HttpRequest, WebResponse and
    >
    > so on. The problem Iam having is to navigate thru
    > multiple pages. I have to login
    >
    > to the web site, choose an option from the list box and
    > then click on the download
    >
    > button. Iam able to login to the site, while choosing the
    > selections from the drop
    >
    > down and posting to the site it is lossing its connection
    > state. In short Iam not
    >
    > able to use the same WebRequest object to post the next
    > URL. The web server is
    >
    > holding a session with my connection and Iam not able to
    > go to the next page using
    >
    > the same object, I have to create a new object with the
    > next URL which would loose
    >
    > the session state. Any help would be appreciated.[/color]

    Vasu,

    HTTP session management is not bound to connections or a specific
    WebRequest instance. It's simply based on session identifiers that are
    sent back to the client (i.e. your application), either as a cookie, in
    a hidden field or as part of the URL. You need to analyze how the web
    site works and make sure you send your session id with each
    subsequent request (as cookie, in the URL or as a request parameter).

    Remember that you have assign a CookieContainer instance to
    HttpWebRequest' s CookieContainer property, otherwise cookies are
    completely ignored.

    Here's a somewhat abstract example

    string loginUrl = "http://host/webapp/login";
    string downloadUrl = "http://host/webapp/download";

    // Assuming the webapp uses cookies!
    HttpWebRequest loginRequest =
    (HttpWebRequest ) WebRequest.Crea te(loginUrl);
    loginRequest.Co okieContainer = new CookieContainer ();
    HttpWebResponse loginResponse =
    (HttpWebRespons e) loginRequest.Ge tResponse();
    // Get the session id and other cookies set by the web app
    CookieCollectio n cookies = loginResponse.C ookies;

    HttpWebRequest downloadRequest =
    (HttpWebRequest ) WebRequest.Crea te(downloadUrl) ;
    // Make sure to send the cookie(s) back to the webapp.
    downloadRequest .CookieContaine r = new CookieContainer ();
    downloadRequest .CookieContaine r.Add(cookies);
    // Add post data that makes webapp download the file
    HttpWebResponse downloadRespons e =
    (HttpWebRespons e) downloadRequest .GetResponse();
    // Stream response bytes to a buffer or file.

    Cheers,
    --
    Joerg Jooss
    joerg.jooss@gmx .net

    Comment

    Working...