473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 5818
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Pedro Fonseca | last post by:
Greetings! In my PHP website I'm trying to raise a File Download dialog directly in a WindowsCE IE client with the following code: header( "Content-Type: application/octet-stream\n" );...
4
by: - - Pierre | last post by:
Hi all, I'm trying to come up with a script and I'm having a heck of a time... I require users to enter the size (in MB or KB) of the attachment they wish to transfer/upload. As they enter...
3
by: Skwish | last post by:
Hi, I would like to download all the files from a folder (of various types i.e. ..txt, .dll, .dat, etc.). I tried Dim MyDir As IO.Directory Dim path As String = Me.MapPath(".") & "\Data" Dim...
2
by: Sleepy | last post by:
I am wanting to allow a client to download a file from a web server and save it on their local PC. The only way that I have found to do this is to use HTTP headers. I am using the code shown below...
4
by: hoenes1 | last post by:
Hi, in my ASP.NET App, I have a list of files which the user can download on click (using Response.BinaryWrite()). the problem is, that once a download has started, the user can't navigate in...
5
by: Stanav | last post by:
Hello all, Thanks in advance for any replies... Now, my question is: Is it possible to do a multiple files download for a single response event on an aspx page? If there is, please give me some...
2
by: xander.zone.3x | last post by:
How can we download data in tables in a html page into a csv file using client side Javascript. I should click a button and a table's data should be downloaded into a csv in local drive. The os...
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.