C# WebClient, Post and Submit

Posted on 2011-10-16
Last Modified: 2012-06-27
I'm learning about WebClient by using it to collect some data from a web page.  I've attached the page source as I see it through FireFox.

I want to post a zip code to the page, and then press the search button.

I think (but not sure) I'm posting the data correctly.  I don't see how to press the search button.

Here is my current code:
using System;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Text;

class Demo {
   static void Main(string[] args) {
      NameValueCollection nvc = new NameValueCollection();
      String response = TestPost("",nvc);

   static String TestPost(String url,NameValueCollection nvc) {
      WebClient webClient = new WebClient();
      byte[] response = webClient.UploadValues(url,"POST",nvc);
      return System.Text.Encoding.UTF8.GetString(response);

Question by:josgood
    LVL 74

    Accepted Solution

    You have to understand how the web works. Buttons, text boxes, drop-down lists...  those are conveniences for users. You computer (and the remote computer) only care about data coming across the wire. To post to a page programmatically, you would not "click" the submit button; rather you would craft a message similar to that which your browser sends when you click the submit button.

    For example, if you look at the source code for the page, the submit button is contained within a <form> tag. This <form> tag has an action. For web forms, the action attribute specifies to which page you are going to send data. In this case, the data will go to the default page located at "/cgi-bin/memberinfo" (either that, or that URL is rewritten on the server to some other URL, but you wouldn't know this unless you maintained that server). What you need to do is mimic this behavior.

    How does one do that? Well, you've got a good chunk of the code set up already. You will be passing values back to the site, and you will be using the POST method, but you need to change the URL that you are pointing to. If I am not mistaken, it should be:

    String response = TestPost("",nvc);

    Open in new window

    This is because the URL in the action attribute is rooted (starts with a forward slash), so it should be found at root directory of the site. I haven't tested the code, but I believe that is all you should need to change.
    LVL 13

    Author Comment

    Fabulous!  Exactly what I was looking for!

    Your explanation is very clear and I appreciate it.  I did not understand the action attribute.  Now I know a bit more and I will read up on it.

    Thank you very much for your help.

    LVL 13

    Author Comment

    BTW, your fix works!
    LVL 74

    Expert Comment

    by:käµfm³d 👽
    NP. Glad to help  = )

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    In my previous two articles we discussed Binary Serialization ( and XML Serialization ( In this article we will try to know more about SOAP (Simple Object Acces…
    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now