[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 395
  • Last Modified:

trying to use WebRequest / WebClient to fill a form over the web consisting of multiple clicks

Hi all,

I've been having a little problem with the WebClient class, and am hoping someone can help.
Here is what I'm currently working with:


string uri = "www.exampleURL.com";

byte[] postBytes = Encoding.ASCII.GetBytes(postString_Begin);
byte[] postBytes2 = Encoding.ASCII.GetBytes(postString2);

WebClient myClient = new WebClient();
myClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

byte[] response = myClient.UploadData(uri, postBytes);

string myresponse = Encoding.ASCII.GetString(response);

response = myClient.UploadData(uri2, postBytes2);

Here is where I hit my wall.
After I upload the first queryString, the next step in the process is returned.
The URL does not change, but when I go to post the next section of values, it posts them back to the first page/form part.
So, basically I'm filling a form on my website, and then taking the values and using these to complete a form on another site, then parsing the result and using what is relevant.  The form on the other site is all on the same page, but goes through roughly 4 button clicks, or Posts.  When I try and run the second, it posts to the first.  I've looked through the WebRequests and WebClient classes, but haven't seen how to keep everything in sequence.
Can anyone point me to How I can post my second querystring to the returned page from the first request?

Thanks
 
 
 
0
btruby
Asked:
btruby
  • 3
  • 3
1 Solution
 
RimvisCommented:
Hello btruby,

>> response = myClient.UploadData(uri2, postBytes2);

uri2? Isn't it a typo?


Regards,

Rimvis
0
 
btrubyAuthor Commented:
yes, the uri2 is supposed to be just uri in this case.
It is still just posting to the first step, and not continuing on ...
any ideas
0
 
RimvisCommented:
>>So, basically I'm filling a form on my website, and then taking the values and using these to complete a form on another site

What do you mean by "another site"? I don't see any other URL in you code.  

The code you provided post twice to the same page. Maybe you are missing something?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
btrubyAuthor Commented:
Here's the code as is (a little stripped down)

postString_Begin = "HomeCountry=USA&MemberType=P&DistID=&CurrPage=ApplicationType&NextPage=ApplicationForm&NextButton=Continue";
string postString2 = "EnrollerID=170138&ApplicantTaxID=&ApplicantNativeName=&ApplicantFirstname=Test&ApplicantMidInitial=T&ApplicantLastname=Testerman&BirthMonth=&BirthDay=&BirthYear=&CoNativeName=&CoFirstname=&CoMidInitial=&CoLastname=&CompanyName=&MailAddress1=2701+Dallas+Pkwy&MailAddress2=Suite+330&MailCity=Plano&MailState=TX&MailPostalCode=75093&MailCityLimits=I&ShipAddress1=2701+N.+Dallas+Pkwy&ShipAddress2=Suite+100&ShipCity=Plano&ShipState=TX&ShipPostalCode=75093&ShipCityLimits=I&NightPhone=1112223434&DayPhone=1112223535&CellPhone=2225556767&Fax=3334567878&Email=metman71@hotmail.com&CorporateMail=Y&BroadcastMail=Y&IAgree=on&VOIAgree=Y&Product_1=GXL&ProductQty_1=1&Product_2=MAXWLXSYSTEM&ProductQty_2=0&Product_3=MAXWLX&ProductQty_3=0&Product_4=MAXNFUZE&ProductQty_4=0&Product_5=GXLVC&ProductQty_5=0&ShipVia=3&ASProduct_1=GXL&ASProductQty_1=1&ASProduct_2=MAXWLX&ASProductQty_2=0&ASProduct_3=MAXNFUZE&ASProductQty_3=0&ASProduct_4=GXLVC&ASProductQty_4=0&ASDay=25&ASStartDate=20090426&ASShipVia=3&CurrPage=ApplicationForm&NextPage=ApplicationPayment&NextButton=Continue";
string uri = "https://nettrax.myvoffice.com/maxintl/Application/Apply.cfm?EnrollerId=170138";
byte[] postBytes = Encoding.ASCII.GetBytes(postString_Begin);
byte[] postBytes2 = Encoding.ASCII.GetBytes(postString2);
WebClient myClient = new WebClient();
myClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
byte[] response = myClient.UploadData(uri, postBytes);
string myresponse = Encoding.ASCII.GetString(response);
response = myClient.UploadData(uri, postBytes2);

as you can see, postString_Begin submits input for the first step, and then postString2 has simulated values for the second step in the form.
Basically, I'm filling out this order form on my site, so I can redesign the cart, and then taking the input and passing it through to the other webserver.  I've got everything in place, except I can't seem to get to post to the second step.  All through out, the url doesn't change, excpet the parameter falls off after the first step.
0
 
RimvisCommented:
Try WebRequest:

        string uri = "www.exampleURL.com";
 
        PostData(uri, postString_Begin);
        PostData(uri, postString2);
 
 
 
        string PostData(string uri, string data)
        {
            WebRequest req = WebRequest.Create(uri);
            req.Method = "Post";
            req.ContentType = "application/x-www-form-urlencoded";
 
            Stream s = req.GetRequestStream();
 
            byte[] bytes = Encoding.ASCII.GetBytes(data);
            s.Write(bytes, 0, bytes.Length);
            s.Close();
 
            StreamReader r = new StreamReader(req.GetResponse().GetResponseStream());
 
            return r.ReadToEnd();
        }

Open in new window

0
 
btrubyAuthor Commented:
Thanks Rimvis,
WebRequest is working as needed once I got the cookies handled.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now