471,853 Members | 858 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,853 software developers and data experts.

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
Nov 15 '05 #1
1 5575
"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.

Similar topics

2 posts views Thread by Pedro Fonseca | last post: by
4 posts views Thread by - - Pierre | last post: by
3 posts views Thread by Skwish | last post: by
2 posts views Thread by Sleepy | last post: by
4 posts views Thread by hoenes1 | last post: by
5 posts views Thread by Stanav | last post: by
NeoPa
reply views Thread by NeoPa | last post: by
reply views Thread by YellowAndGreen | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.