I have been reading up on using webrequests and post and get methods to submit an online form but I am very lost. The webform resides on a page full of other hyperlinks, but viewing the source code it says method=get in one spot and method=post in another. The method=get is located in the code near information regarding a search bar though so I believe that applies to that function. The 2 text fields I want to fill before submitting are listed as “name=weblink” and “name=imageurl”, so lots of the examples I’ve seen using id values doesn’t seem to apply. The “Save” button is identified as
input type="submit" value='Save Changes'
Currently this is what my code looks like:
try
{
//store our text box information as variables to be uploaded
string picture = txtbxPicture.Text,
weblink = txtbxWeblink.Text,
uriString = txtbxWebLink.Text;
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
// Create a new NameValueCollection instance to hold some custom parameters to be posted to the URL.
NameValueCollection collection =
new System.Collections.Specialized.NameValueCollection();
collection.Add("weblink", weblink);
collection.Add("imageurl", picture);
byte[] result = myWebClient.UploadValues(uriString, collection);
}
catch (FormatException ex)
{
MessageBox.Show("Error: " + ex.Message);
}
If I understand everything correctly I’m using the NameValueCollection to look for fields based on their “name”? This code was compiled using primarily MSDN’s info here: http://msdn.microsoft.com/en-us/library/9w7b4fz7.aspx
I have seen a lot of other examples that use encoding, and explicitly identify that it is a “POST” method, but MSDN says it’s implied? I’m basically lost here. Can someone offer a little guidance, the current threads out there are just making me more confused.
UPDATE – So identifying post doesn’t seem to be necessary, but is possible. What about clicking the submit button is that something that I need to be coding in but am not? I don’t understand what my code above needs to do in order to complete the process.
WebClient.UploadValues()will use POST over HTTP by default, you don’t have to specify that (you may though if you need to, there’s an overload of the method that does allow you to).From MSDN: