Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# HttpWebRequest Post Method many parameters in form

I am writing a simple C# program to do some web request and posting data. I understand how the basics work like how to login with password and stuff of a html form. However I am wondering if there is a lot of input parameters (such as this question page ) like check boxes and text fields, is there any efficient method than hard-coding 20 parameters in a string and passing it in? I can read the html file parse it and scan out the input and use String builder to make such a string but I am wondering is there a more efficient method than doing that?

    private HtmlAgilityPack.HtmlDocument getpage(string url ,String input)
    {
        try
        {
            Stream datastream;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.CookieContainer = new CookieContainer();
            request.CookieContainer.Add(cookies);
            request.AllowAutoRedirect = true;
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
            request.ContentType = "application/x-www-form-urlencoded";

            if (input!=null)
            {
                String postData = "";
                request.Method = "POST";
                if (input == "login")
                {
                    postData = String.Format("username={0}&password={1}", "myusername", "mypassword");
                }
                else if (input == "sendMessage")
                {
                //THIS IS THE LONG STRING THAT I DON'T WANT TO HARD CODE
                    postData = String.Format("reciever={0}&sendmessage={1}", "thepersontomessage" ,this.DefaultMessage);
                //I am just puting two parameters for now, there should be alot
                }
                byte[] byteArray = Encoding.UTF8.GetBytes(postData);
                request.ContentLength = byteArray.Length;
                datastream = request.GetRequestStream();
                datastream.Write(byteArray, 0, byteArray.Length);
                datastream.Close();
            }

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            datastream = response.GetResponseStream();
            String sourceCode = "";
            using (StreamReader reader = new StreamReader(datastream))
            {
                sourceCode = reader.ReadToEnd();
            }

            HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
            htmlDoc.LoadHtml(sourceCode);
            this.cookies.Add(response.Cookies);
            return htmlDoc;
        }
        catch (Exception)
        {
            return null;
        }

Also is there a simple way to see what are the parameter values set to when I click a button on a html form within a browser (basically the post url string and parameter values that is sent) so I can hardcode those values into the Postdatastring (check boxs, texts etc)

like image 878
noobiehacker Avatar asked Feb 02 '26 08:02

noobiehacker


1 Answers

Personally what I would do is build the parameters as a Dictionary<string,string> so that you can just do:

var parms = new Dictionary<string,string>();

parms.Add("username","fred");

You can then have a method such as:

string DictToString(Dictionary<string,string> dict)
{
   StringBuilder builder = new StringBuilder();

   foreach(KeyValuePair<string,string> kvp in dict) {
      builder.Append(kvp.Key + "=" + kvp.Value + "&");
   }

   return builder.ToString();
}

You can then get the final parameter string with:

var parms_str = builder.ToString();
like image 75
Lloyd Avatar answered Feb 03 '26 21:02

Lloyd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!