Simulate submit button click using WebRequest

Hi, I'm sure there are tons of examples as to how to post data programatically using C#/WebRequest request but I have not found any luck yet with posting data to the URL below.

Please see how I've coded the necessary form fields into the code review box. I think this may work if receiving form parses Query String variables. So the question is how I can accomplish this using C# code? Any help greatly appreciated

Url I'm trying to post to: http://www.justcover.ie/
string formFields;
            
        formFields = "stay=2-4 months";
        formFields += "&%%Surrogate_stay=1";
        
        
        formFields += "&%%Surrogate_destination=1";
        formFields += "&%%Surrogate_destination_st=1";
        formFields += "&%%Surrogate_destination_bp=1";

        ////Destination 
        formFields += "&destination_bp=Europe";
        
        ////Cover type
        formFields += "&type=Annual Multi Trip";

        ////Date of Travel 
        formFields += "&datetravel=11/04/2010";

        formFields += "&%%Surrogate_type=1";
        formFields += "&step=1";
        formFields += "&__Click=1";
        formFields += "&datereturn=";

        ////Type of policy 
        formFields += "&covertype=A";//A: Single, C:Family, B: Couple, D: Single Parent Family
        formFields += "&%%Surrogate_covertype=1";

        ////Adults age categories [A:18 - 65, B:66 - 69, C:70 - 75]
        formFields += "&agebracket=A";
        formFields += "&%%Surrogate_agebracket=1";
        
        ////Have all insured private health
        formFields += "&hi=No";
        formFields += "&%%Surrogate_hi=1";

        ////Discount code if applicable 
        formFields += "&discountinput=";

        ////Where did you hear about us? 
        formFields += "&heard=Word of Mouth";
        formFields += "&%%Surrogate_heard=1";

Open in new window

ExcUsr2008Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
ExcUsr2008Connect With a Mentor Author Commented:
No responses ... I think there is no solution ...
0
 
käµfm³d 👽Commented:
Perhaps the following will work for you.
private void button1_Click(object sender, EventArgs e)
{
    System.Net.WebClient poster = new System.Net.WebClient();
    byte[] response;

    poster.QueryString["stay"] = "2-4 months";
    poster.QueryString["Surrogate_stay"] = "1";

    poster.QueryString["Surrogate_destination"] = "1";
    poster.QueryString["Surrogate_destination_st"] = "1";
    poster.QueryString["Surrogate_destination_bp"] = "1";

    ////Destination 
    poster.QueryString["destination_bp"] = "Europe";

    ////Cover type
    poster.QueryString["type"] = "Annual Multi Trip";

    ////Date of Travel 
    poster.QueryString["datetravel"] = "11/04/2010";

    poster.QueryString["Surrogate_type"] = "1";
    poster.QueryString["step"] = "1";
    poster.QueryString["__Click"] = "1";
    poster.QueryString["datereturn"] = "";

    ////Type of policy 
    poster.QueryString["covertype"] = "A";   //A: Single, C:Family, B: Couple, D: Single Parent Family
    poster.QueryString["Surrogate_covertype"] = "1";

    ////Adults age categories [A:18 - 65, B:66 - 69, C:70 - 75]
    poster.QueryString["agebracket"] = "A";
    poster.QueryString["Surrogate_agebracket"] = "1";

    ////Have all insured private health
    poster.QueryString["hi"] = "No";
    poster.QueryString["Surrogate_hi"] = "1";

    ////Discount code if applicable 
    poster.QueryString["discountinput"] = "";

    ////Where did you hear about us? 
    poster.QueryString["heard"] = "Word of Mouth";
    poster.QueryString["Surrogate_heard"] = "1";

    response = poster.UploadValues("http://www.justcover.ie/", poster.QueryString);

    MessageBox.Show(Encoding.ASCII.GetString(response));
}

Open in new window

0
 
ExcUsr2008Author Commented:
Thank you for your response. I've tried your code but no luck yet. It's not working. I'm not even receiving any response. If you view the source code of that URL the actual URL that the button click is posting to is this: http://www.justcover.ie/justcover/cover2.nsf/quotation?OpenForm&Seq=1

I understand that your code is posting a QueryString with the form fields but I think it works when the receiving form is handling the query string parameters from the URL, which may not be the case with the target URL we are trying to post to.

hmmm... there has to be  a way ...
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
käµfm³d 👽Commented:
I modified my earlier version to write out the response to a local file. Following is my (slightly) new code and attached is an image of the file that was generated. You can see from the address bar that the file is local to my machine.
private void button1_Click(object sender, EventArgs e)
{
    this.button1.Enabled = false;

    System.Net.WebClient poster = new System.Net.WebClient();
    byte[] response;

    poster.QueryString["stay"] = "2-4 months";
    poster.QueryString["%%Surrogate_stay"] = "1";

    poster.QueryString["%%Surrogate_destination"] = "1";
    poster.QueryString["%%Surrogate_destination_st"] = "1";
    poster.QueryString["%%Surrogate_destination_bp"] = "1";

    ////Destination 
    poster.QueryString["destination_bp"] = "Europe";

    ////Cover type
    poster.QueryString["type"] = "Annual Multi Trip";

    ////Date of Travel 
    poster.QueryString["datetravel"] = "11/04/2010";

    poster.QueryString["%%Surrogate_type"] = "1";
    poster.QueryString["step"] = "1";
    poster.QueryString["__Click"] = "1";
    poster.QueryString["datereturn"] = "";

    ////Type of policy 
    poster.QueryString["covertype"] = "A";   //A: Single, C:Family, B: Couple, D: Single Parent Family
    poster.QueryString["%%Surrogate_covertype"] = "1";

    ////Adults age categories [A:18 - 65, B:66 - 69, C:70 - 75]
    poster.QueryString["agebracket"] = "011";
    poster.QueryString["%%Surrogate_agebracket"] = "1";

    ////Have all insured private health
    poster.QueryString["hi"] = "Quinn Healthcare Essential Plus";
    poster.QueryString["%%Surrogate_hi"] = "1";

    ////Discount code if applicable 
    poster.QueryString["discountinput"] = "";

    ////Where did you hear about us? 
    poster.QueryString["heard"] = "Word of Mouth";
    poster.QueryString["%%Surrogate_heard"] = "1";

    response = poster.UploadValues("http://www.justcover.ie/justcover/cover2.nsf/quotation?OpenForm&Seq=1", poster.QueryString);

    using (System.IO.StreamWriter writer = new System.IO.StreamWriter("test.html"))
    {
        writer.Write(Encoding.ASCII.GetString(response));
    }

    this.button1.Enabled = true;
}

Open in new window

Untitled.jpg
0
 
ExcUsr2008Author Commented:
Hi, thank you so much for the illustration. The response you saved in a file, is not what the form would return if we manually submit this form on the website. So what we are missing here is the 'click' event. Not sure how we can simulate that click event to get the actual response. The response would include a different page with pricing details - this is what I need to see. thanks again for your help.
0
 
käµfm³d 👽Commented:
Sorry. I just tried it on the site and I now realize that is not what you are after  :(

I'm looking into it further.
0
 
ExcUsr2008Author Commented:
Thank you ...
0
All Courses

From novice to tech pro — start learning today.