Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Downloading a Web Page Programmatically - .NET Framework

RSS
Modified on Wed, Aug 17, 2011, 8:27 AM by Administrator Categorized as ASP·NET Web Forms, ·Net Framework
public static void PostData(string localFile)
{
    var targetUrl = @"http://www.zip-codes.com/zip-code-radius-finder.asp";
    var parameters = "zipMilesLow=0&zipMilesHigh=50&zip1=30303";

    //--- Build the request object ---
    var request = System.Net.WebRequest.Create(targetUrl);
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    request.ContentLength = parameters.Length;

    //--- Add the parameters to the request ---
    var sw = new System.IO.StreamWriter(request.GetRequestStream());
    sw.Write(parameters);
    sw.Close();

    //--- Get the response object ---
    var response = request.GetResponse();
    var sr = new System.IO.StreamReader(response.GetResponseStream());
    System.IO.File.WriteAllText(localFile, sr.ReadToEnd());
}

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2018, Patrick Jasinski.