Solved

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

Posted on 2009-04-01
6
371 Views
Last Modified: 2012-05-06
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
Comment
Question by:btruby
  • 3
  • 3
6 Comments
 
LVL 19

Expert Comment

by:Rimvis
ID: 24046688
Hello btruby,

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

uri2? Isn't it a typo?


Regards,

Rimvis
0
 

Author Comment

by:btruby
ID: 24046775
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
 
LVL 19

Expert Comment

by:Rimvis
ID: 24046914
>>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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:btruby
ID: 24047047
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
 
LVL 19

Accepted Solution

by:
Rimvis earned 125 total points
ID: 24047413
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
 

Author Comment

by:btruby
ID: 24051773
Thanks Rimvis,
WebRequest is working as needed once I got the cookies handled.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now