Question : Webrequest POST

I'm having a bit of a problem with sending POST messages to webpages.
I want a program that is capable of editing posts on a vBulletin forum.

I already have a piece of code that uses Webrequest to login and store the CookieContainer. So I can get to a new post page while logged in without problems. The problem is I can't post any data in the textboxes. Or maybe I can but it doesn't show it in the response which I set to a Webbrowser object.

What am I doing wrong? Here's my code, not with a forum but just EE site which doesn't work either. I'm trying to post "testing" to the searchbox in the top right, the name is "gsearchBox" and id is "q". Do I need to use the name or the id of the element?

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
           // Create a request using a URL that can receive a post.
            var request = (HttpWebRequest)WebRequest.Create("http://www.experts-exchange.com/");
            request.UserAgent =
    "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";

            // Set the Method property of the request to POST.
            request.Method = "POST";

            // Create POST data and convert it to a byte array.
            string postData = "gsearchBox=testing";               //<------ NAME OF THE SEARCHBOX
            //string postData = "q=testing2";                       <------ ID OF THE SEARCHBOX

            var encoding2 = new ASCIIEncoding();
            byte[] byteArray = encoding2.GetBytes(postData);

            ((HttpWebRequest)request).CookieContainer = cookiecontain;
            // Set the ContentType property of the WebRequest.
            request.ContentType = "application/x-www-form-urlencoded";
            // Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length;
            request.KeepAlive = true;
            // Get the request stream.
            Stream dataStream = request.GetRequestStream();
            // Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length);
            // Close the Stream object.
            dataStream.Close();
            // Get the response.
            WebResponse response = request.GetResponse();
            // Display the status.
            Console.WriteLine(((HttpWebResponse)response).StatusDescription);
            // Get the stream containing content returned by the server.
            dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader2 = new StreamReader(dataStream);
            // Read the content.
            string responseFromServer2 = reader2.ReadToEnd();
            // Display the content.
            webBrowser1.DocumentText = responseFromServer2;
            // Clean up the streams.
            reader.Close();
            dataStream.Close();
            response.Close();




Secondary question, how can I simulate a submit button? After I finished editing the post, to submit the changes?
Thanks in advance.

Answer : Webrequest POST

It must be the name of the search box.

If you check the HTML for EE, there are a bunch of other hidden fields that are sent along with the text typed by the user. You do not know whether any of those are checked by the server and an absence of those may cause the server to return error or just silently return to the page. Who knows ...

You should try by adding all the form fields in your search data. A quick way is to start WireShark and capture a sample search and examine the data sent to the server. Just copy the data part and paste it in your program to do a quick check.

As for the submit, there's really nothing to do. Just post the form data to the URL specified in the "action" attribute of the form. If there's none just post to the current page (PostBack). Also, how you post the data (the format) depends upon the "encoding" attribute of the Form. If it's www-url-encoded, it's like a query-string with url encoded keys and values.

If it's multipart/form-data, then thats a whole diferent story. I believe for your case, it's mostly going to be the www-url-encoded type.

Hope that helps ...

Random Solutions  
 
programming4us programming4us