December 2007
Intermediate to advanced
896 pages
19h 57m
English
You need to send a collection of name-value pairs to simulate a form being executed on a browser to a location identified by a URL.
Use the System.Net.WebClient class to send a set of name-value pairs to the web server using the UploadValues method. This class enables you to masquerade as the browser executing a form by setting up the name-value pairs with the input data. The input field ID is the name, and the value to use in the field is the value:
using System;
using System.Net;
using System.Text;
using System.Collections.Specialized;
// In order to use this, you need to run the CSCBWeb project first.
Uri uri = new Uri("http://localhost:7472/CSCBWeb/WebForm1.aspx");
WebClient client = new WebClient();
// Create a series of name/value pairs to send
// Add necessary parameter/value pairs to the name/value container.
NameValueCollection collection = new NameValueCollection()
{ {"Item", "WebParts"},
{"Identity", "foo@bar.com"},
{"Quantity", "5"} };
Console.WriteLine("Uploading name/value pairs to URI {0} ...",
uri.AbsoluteUri);
// Upload the NameValueCollection
byte[] responseArray =
client.UploadValues(uri.AbsoluteUri,"POST",collection);
// Decode and display the response.
Console.WriteLine("\nResponse received was {0}",
Encoding.ASCII.GetString(responseArray));The WebForm1.aspx page, which receives and processes this data, looks like this:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebForm1.aspx.cs" Inherits="WebForm1" ...
Read now
Unlock full access