Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I�m looking for some code snippets on how to identify and change the value of a text box within a webpage in VB.NET and Internet Explorer. I�m using the axWebBrowser control on my form to display the web page and then do a directcast of my axWebBrowser to get my HTML doc.

 

Dim doc As mshtml.HTMLDocument = DirectCast(Me.AxWebBrowser1.Document, mshtml.HTMLDocument)

 

If I navigate to a web page, let�s take http://www.yahoo.com for example, how can I determine how the search text box is referenced and how I could send keystrokes to that search text box in my axWebBrowser control?

 

Any thoughts on this would be appreciated

Thanks in advance

Ben

Posted
Not sure what you're trying to accomplish here, but it would probably be easier and more effective to emulate the HTML form in a windows form and then utilze the HttpWebRequest class to POST data to the form.
Posted

Thanks for the quick response...

 

We have data entriest performing web lookups. My VB app pulls down the 'next guy to lookup' from the mainframe; to verify if he's got valid insurance coverage. The user will then enter VIN and policy number to a web page (my axWebBrowser) If the guy has valid insurance do this...else do this...

 

So idealy I would like to automatically plug in the VIN and Policy number into the corresponding text boxes on Allstates website, or Statefarms website, etc...

Does this help?

 

Could you elaborate on the HttpRequest, please.

 

Ben

Posted
Could you elaborate on the HttpRequest' date=' please.[/quote']When you POST data using an HTML form it is sent to the remote server as a QueryString. Example: txtFirstName=Joe&txtLastName=Johnson&Age=32&UID=123456. The fields "txtFirstName", "txtLastName", etc are the names of the HTML form fields. The HttpRequest essentially emulates this action and builds the string in a similar manner to a web browser. The benefit, of course, is that you can do this in your own application instead of trying to capture content and such from a web browser.

 

string postData = "[color=Red]txtFirstName=Joe&txtLastName=Johnson&Age=32&UID=123456[/color]";

HttpWebRequest webRequest = WebRequest.Create("[b][url]http://yourwebsite.com[/url][/b]") as HttpWebRequest;

// create a POST
webRequest = WebRequest.Create(URL) as HttpWebRequest;
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";

// write the response data to the stream
StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream());
requestWriter.Write(postData);
requestWriter.Close();

// read the HTML that is sent back
StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
string responseData = responseReader.ReadToEnd();
responseReader.Close();

// use a regular expression to get the information you need

Posted

An unhandled exception of type 'System.Net.WebException' occurred in system.dll

 

Additional information: The underlying connection was closed: Could not establish trust relationship with remote server.

 

That is the error I get when this line of code is executed:

Dim requestWriter As StreamWriter = New StreamWriter(webRequest.GetRequestStream())

 

Does this code build my variables into the HTTP address? Because when I manually navigate through the website and enter in policy and vin info and hit the search button it does not display my search criteria in the address...

 

I don't know if I'm making any sense but if you could give me a few more pointers that would be great!

 

Thanks again

Ben

Posted
Does this code build my variables into the HTTP address? Because when I manually navigate through the website and enter in policy and vin info and hit the search button it does not display my search criteria in the address...
Since the form method is POST, the "search criteria" will not be seen in the URL. It is embedded into the HTTP request.

 

 

That is the error I get when this line of code is executed:

Dim requestWriter As StreamWriter = New StreamWriter(webRequest.GetRequestStream())

You need to make sure that you change the URL in the example from "yourwebsite.com" to be the exact page that the HTML form resides on.

 

Another thing that can give you problems is if you are not using the exact HTML field names. To find these you need to look at the HTML source code for the page you are posting to. Having invalid fields in the QueryString won't generate the error you displayed, but it might make it so that the information you're submitting is not accepted.

Posted
Okay, so I tried your sample code on a different website, cars.com and used "locationzip" as the web text box field to test this with. I get no errors, however I'm not quite sure what to do next. If I perform an HTTPWebRequest on locationzip and plug in a zipcode, isn't that like simulating if the user were to enter in the zipcode and hitting the enter key?
Posted
The website that you enter should be the URL of the page specified in the ACTION portion of the FORM tag. On cars.com it looks like this is the page "http://siy.cars.com/siy/EditAd". Also, it looks like that form on cars.com is using a "GET" method istead of a "POST" method. If you change "POST" to "GET" in the code sample I gave you it should work. The only difference is that the QueryString is not embedded into the HTTP request. It is in the URL instead.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...