?
Solved

C# WebClient, Post and Submit

Posted on 2011-10-16
4
Medium Priority
?
2,214 Views
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();
      nvc.Add("Zip","92274");
      String response = TestPost("http://www.leginfo.ca.gov/yourleg.html",nvc);
      Console.WriteLine("done");
   }

   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);
   }
}

temp.html
0
Comment
Question by:josgood
  • 2
  • 2
4 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 36976247
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("http://www.leginfo.ca.gov/cgi-bin/memberinfo",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.
0
 
LVL 13

Author Comment

by:josgood
ID: 36976283
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.

Joe
0
 
LVL 13

Author Comment

by:josgood
ID: 36976288
BTW, your fix works!
0
 
LVL 75

Expert Comment

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

807 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