http file download

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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();
 
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,

www.domain.com/sample.asp?field1=value1&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.
 
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");
 
Back
Top