473,738 Members | 8,848 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5879
"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.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
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
2140
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" ); header( "Content-Disposition: attachment; filename=\"$file\"\n" ); header( "Content-Transfer-Encoding: binary\n" ); header( "Content-Length: " . filesize($path.$file) . "\n" );
4
5661
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 the number and move over to the next field (using onBlur or...), I would like a popup/alert window warning them that this attachment size would take "x" amount of time based on a speed of 24 kilobits per second (kbps). If they are happy with the...
3
2324
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 dirs As String() = IO.Directory.GetFiles(path) Dim iLoop As Integer Dim FileName As String 'Set the appropriate ContentType.
2
1945
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 (which I have copied with some variable name changes) from a book on ASP.NET programming. This code sits in the Page_Load event of a file called 'Download.aspx'. In order to send a file to the client, this page is called from another page (using...
4
1848
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 the window because the response is sending the file and so no new requests can be made. what possibilities do i have to make the client able to browse other pages in my app while downloading a file? Thanks in advance.
5
3677
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 directions/sample code or anything. On my web application, I have a CheckBoxList that shows all the files. The user will check (select) the files to download, then click on the "Download" button. A SaveAs dialog pops up to allow the user to...
2
15720
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 used is m$ windows..
2
6964
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 attending interviews. If you own a company best way to judge if the candidate is worth of it. http://www.questpond.com/InterviewRatingSheet.zip
1
47476
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 on a link and after a moment or two a file download dialog box pops-up in your web browser and prompts you for some instructions, such as “open” or “save“. I’m going to show you how to do that using a perl script. What You Need Any recent...
0
8969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8788
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9476
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9335
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.