By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,696 Members | 2,213 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,696 IT Pros & Developers. It's quick & easy.

Download file using c# client

P: n/a
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
Nov 15 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Vasu" wrote:
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.


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.Create(loginUrl);
loginRequest.CookieContainer = new CookieContainer();
HttpWebResponse loginResponse =
(HttpWebResponse) loginRequest.GetResponse();
// Get the session id and other cookies set by the web app
CookieCollection cookies = loginResponse.Cookies;

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

Cheers,
--
Joerg Jooss
jo*********@gmx.net
Nov 15 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.