473,385 Members | 1,647 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.

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 2782
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,...
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.