473,396 Members | 2,018 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,396 software developers and data experts.

http file download

Hi,

I am trying to download a file from a web page in code. I cannot use the System.Net.WebClient DownloadFile method because in order to get the file i must POST a request with form fields. Took a wild-*ss_guess at how to do it but it doesn't work. (see code below) I get a error 400 response (bad request). Any advice on this would be greatly appreciated.

WebRequest request =
System.Net.WebRequest.Create("http://oaspub.epa.gov/pls/ats/arr_txt");
WebResponse response;
NameValueCollection formCollection = new NameValueCollection();
Stream stream;
FileStream fs;
byte[] buffer;
int copyByte = 0;

fs = File.Create(@"C:\aaa.txt");

formCollection.Add("STATE_CD","USA");
formCollection.Add("LOWID","000000000000");
formCollection.Add("HIGHID","999999999999");
formCollection.Add("ACCTNAME","%");
formCollection.Add("REPNAME","%");

buffer = System.Text.Encoding.ASCII.GetBytes(formCollection .ToString());

request.Method = "POST";
request.ContentLength = buffer.Length;
stream = request.GetRequestStream();
stream.Write(buffer,0,buffer.Length);
stream.Close();

stream = request.GetResponse().GetResponseStream();

copyByte = stream.ReadByte();
while(copyByte != -1)
{
fs.WriteByte((byte)copyByte);
copyByte = stream.ReadByte();
}

stream.Close();
fs.Close();

Nov 22 '05 #1
5 2783
The general method for posting as per the RFC is to build a command-line string using the name/value pairs and send it. The string is the same as we've all seen when a URL is passed a querystring. For example,

http://www.domain.com/sample.asp?fie...&field2=value2

Create the request specifying the web page, build the "querystring" and send it via the request stream. Use method of POST and ContentType of application/x-www-form-urlencoded (this is the default type of form post). If using non-ASCII characters you can post using multipart/form-data as defined at http://www.ietf.org/rfc/rfc1867.txt. This type of post is also what's used to upload files.

Hope this helps.

"Ron Slosberg" wrote:
Hi,

I am trying to download a file from a web page in code. I cannot use the System.Net.WebClient DownloadFile method because in order to get the file i must POST a request with form fields. Took a wild-*ss_guess at how to do it but it doesn't work. (see code below) I get a error 400 response (bad request). Any advice on this would be greatly appreciated.

WebRequest request =
System.Net.WebRequest.Create("http://oaspub.epa.gov/pls/ats/arr_txt");
WebResponse response;
NameValueCollection formCollection = new NameValueCollection();
Stream stream;
FileStream fs;
byte[] buffer;
int copyByte = 0;

fs = File.Create(@"C:\aaa.txt");

formCollection.Add("STATE_CD","USA");
formCollection.Add("LOWID","000000000000");
formCollection.Add("HIGHID","999999999999");
formCollection.Add("ACCTNAME","%");
formCollection.Add("REPNAME","%");

buffer = System.Text.Encoding.ASCII.GetBytes(formCollection .ToString());

request.Method = "POST";
request.ContentLength = buffer.Length;
stream = request.GetRequestStream();
stream.Write(buffer,0,buffer.Length);
stream.Close();

stream = request.GetResponse().GetResponseStream();

copyByte = stream.ReadByte();
while(copyByte != -1)
{
fs.WriteByte((byte)copyByte);
copyByte = stream.ReadByte();
}

stream.Close();
fs.Close();

Nov 22 '05 #2
Thanks Todd. That worked great. And the new code is a heck of a lot simpler...

System.Net.WebClient webClient = new WebClient();

webClient.QueryString.Add("STATE_CD","USA");
webClient.QueryString.Add("LOWID","000000000000");
webClient.QueryString.Add("HIGHID","999999999999") ;
webClient.QueryString.Add("ACCTNAME","%");
webClient.QueryString.Add("REPNAME","%");

webClient.Headers.Add("Content-Type: application/x-www-form-urlencoded");

webClient.DownloadFile("http://oaspub.epa.gov/pls/ats/arr_txt",@"C:\bbb.txt");

Nov 22 '05 #3
The general method for posting as per the RFC is to build a command-line string using the name/value pairs and send it. The string is the same as we've all seen when a URL is passed a querystring. For example,

http://www.domain.com/sample.asp?fie...&field2=value2

Create the request specifying the web page, build the "querystring" and send it via the request stream. Use method of POST and ContentType of application/x-www-form-urlencoded (this is the default type of form post). If using non-ASCII characters you can post using multipart/form-data as defined at http://www.ietf.org/rfc/rfc1867.txt. This type of post is also what's used to upload files.

Hope this helps.

"Ron Slosberg" wrote:
Hi,

I am trying to download a file from a web page in code. I cannot use the System.Net.WebClient DownloadFile method because in order to get the file i must POST a request with form fields. Took a wild-*ss_guess at how to do it but it doesn't work. (see code below) I get a error 400 response (bad request). Any advice on this would be greatly appreciated.

WebRequest request =
System.Net.WebRequest.Create("http://oaspub.epa.gov/pls/ats/arr_txt");
WebResponse response;
NameValueCollection formCollection = new NameValueCollection();
Stream stream;
FileStream fs;
byte[] buffer;
int copyByte = 0;

fs = File.Create(@"C:\aaa.txt");

formCollection.Add("STATE_CD","USA");
formCollection.Add("LOWID","000000000000");
formCollection.Add("HIGHID","999999999999");
formCollection.Add("ACCTNAME","%");
formCollection.Add("REPNAME","%");

buffer = System.Text.Encoding.ASCII.GetBytes(formCollection .ToString());

request.Method = "POST";
request.ContentLength = buffer.Length;
stream = request.GetRequestStream();
stream.Write(buffer,0,buffer.Length);
stream.Close();

stream = request.GetResponse().GetResponseStream();

copyByte = stream.ReadByte();
while(copyByte != -1)
{
fs.WriteByte((byte)copyByte);
copyByte = stream.ReadByte();
}

stream.Close();
fs.Close();

Nov 22 '05 #4
Thanks Todd. That worked great. And the new code is a heck of a lot simpler...

System.Net.WebClient webClient = new WebClient();

webClient.QueryString.Add("STATE_CD","USA");
webClient.QueryString.Add("LOWID","000000000000");
webClient.QueryString.Add("HIGHID","999999999999") ;
webClient.QueryString.Add("ACCTNAME","%");
webClient.QueryString.Add("REPNAME","%");

webClient.Headers.Add("Content-Type: application/x-www-form-urlencoded");

webClient.DownloadFile("http://oaspub.epa.gov/pls/ats/arr_txt",@"C:\bbb.txt");

Nov 22 '05 #5
If you are reading this, I am sorry. I am just testing my account.
--
Greg G
"Ron Slosberg" wrote:
Thanks Todd. That worked great. And the new code is a heck of a lot simpler...

System.Net.WebClient webClient = new WebClient();

webClient.QueryString.Add("STATE_CD","USA");
webClient.QueryString.Add("LOWID","000000000000");
webClient.QueryString.Add("HIGHID","999999999999") ;
webClient.QueryString.Add("ACCTNAME","%");
webClient.QueryString.Add("REPNAME","%");

webClient.Headers.Add("Content-Type: application/x-www-form-urlencoded");

webClient.DownloadFile("http://oaspub.epa.gov/pls/ats/arr_txt",@"C:\bbb.txt");

Nov 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Michael Foord | last post by:
I'm creating a CGI that offers files for download. After googling I though I had the right headers to send to force a download - but it sends the data to the browser instead of opening the download...
3
by: Derrick | last post by:
I have an http download that streams a zip file to client, after download I am getting "Error start of central directory not found; Zip file corrupt. Possible casue: file transfer error." Ring...
1
by: Silvia | last post by:
I have this code in web.config file <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="dllredirect"/> <codeBase...
2
by: Derrick | last post by:
Anyone been able to get a multi file HTTP download working with ASP? I have a single file download working fine with the usual setting the content type, file name, stream the file from...
0
by: Chuck Anderson | last post by:
I am writing a Php script to run on my home PC (Windows) that downloads an Apache access log file and inserts new entries into a database.. The only way I can access these log files is through a...
0
by: Owen Jenkins | last post by:
I've been using Dev's fantastic InetTransferLib to upload and download files via ftp. Now I'm using the http procedures to have my application download zip files from my website. Although the...
1
by: Owen Jenkins | last post by:
Reposting this message from last week since I have not had a reply so far. Hopefully someone will have an idea? --- I've been using Dev's fantastic InetTransferLib to upload and download files...
0
by: Buddy Ackerman | last post by:
I am trying to implment a file download via a link such that when clicked, instead of starting the default application for that type of file the user will be presented with a download dialog...
2
by: Dmitry Duginov | last post by:
I wrote HTTP module and configured it to fire with every request to my application. In IIS6 aspnet_isapi.dll mapped as Application Extension, web.config contains the following: <system.web> ...
0
by: Rhys666 | last post by:
Basically I have a link that opens my download page and the querystring identifies the type of 'template' Excel spreadsheet has asked to download. The download page reads the querystring,...
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
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,...
0
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...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.