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(); 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();
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");
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();
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");
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"); This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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>
...
|
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,...
|
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...
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |