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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

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
ExcUsr2008Author Commented:
No responses ... I think there is no solution ...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.