Learn how to a build a cloud-first strategyRegister Now

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

Foxpro Webesite Log in

I have a VFP 8.0 program which needs to automatially log into a website by providing my login name and password.   This is necessary to access certain member only areas which my subscription allows me to see, but not casual users.

It appears that each page looks to see if the user is logged in before presenting the actual data page.  I wonder if this is done with a cookie which needs to be created before actually navigating to the page.  

I am currently using ic=CREATEOBJECT("inetctls.inet") to get a connection to the page(s) using
pagecontent=ic.openurl("http:\\123232abcde?pageparameters=XX

The page opens, but checks something to see if I'm logged in , does not find it and presents an option to login without showing the content of the actual page.

On normal pages, I use STRTOFILE(pagecontents, "e:\THISdata.htm") and then open the file for parsing using   infile=FOPEN("e:\THISdata.htm")


ic=CREATEOBJECT("inetctls.inet")
pagecontent=ic.openurl("http:\\123232abcde?pageparameters=XX
STRTOFILE(pagecontents, "e:\THISdata.htm")
infile=FOPEN("e:\THISdata.htm")
thisline = fgets(infile)
   ....... test if required data in this line
   ....... continue to read in thisline=fgets(infile)
until eof(infile)

This sequence works fine, if the page does not need the user to be looged in.

BTW, I AM a paid subscriber to this website, so I'm not trying to get data which I am not supposed to see.

Please include working code if answering this question, generic answers will not be considered.

Thanks for your help.
0
wheat01
Asked:
wheat01
  • 3
  • 2
  • 2
3 Solutions
 
CaptainCyrilCommented:
You asked for sample code.

This is what I do to fill forms when calling them from VFP:

SET SAFETY OFF
PUBLIC ie AS InternetExplorer.Application
cString = ''
cString = cString + '<html>'
cString = cString + '<body>'
cString = cString + '<form name="form1">'
cString = cString + '<input type="text" id="name" value="">'
cString = cString + '</form>'
cString = cString + '</body>'
cString = cString + '</html>'

= STRTOFILE(cString,'html.htm')

ie = CREATEOBJECT("InternetExplorer.Application")
ie.Visible = .T.
ie.Navigate2(ADDBS(SYS(5)+SYS(2003))+'html.htm')
ie.document.form1.name.value = 'Cyril'

Open in new window


You need to turn off the security in Internet Explorer for this to work properly. Of course you can use other means to access the website like HTTP Request or use a WebBrowser control inside a VFP form which is the easiest of all.
0
 
Olaf DoschkeSoftware DeveloperCommented:
There is one simple solution without changing your code. Login before you use your code, and your user session should be valid. That works for me, when using Internetexplorer.Application or msxml2.xmlhttp

If the login is using BasicAuth, the inetctls.inet Ole class you use has Password and Username properties you can set.

If not you can use Cyrils ie.document.form1.name.value = 'user' and ie.document.form1.password.value = 'password' before submitting the login form. You just need to know the login page, login form name and input tag names to automate them this way.

There's no way to tell from your question, what authentication method is used by the website. BasicAuth is typically determined by being able to add user:passwor@domain.com to the request url and in behaviour will create a popup login window, but that's not a sure sign it's BasicAuth.

No matter what login mode, once you're logged in and your session is valid normal requests will work. There fore again the non code just general answer: Login before using your application, then you don't have to fiddle and find out what code to add for automaitc login.

Bye, Olaf.
0
 
CaptainCyrilCommented:
Once you login no matter how, you need to keep that session alive otherwise the server will think you are another user in different requests.

1) Keep WebBrowser or InternetExplorer open.
2) Keep the handle global and make subsequent calls from same handle.
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.

 
Olaf DoschkeSoftware DeveloperCommented:
Cyril, if a session cookie is not deleted because you close the browser depends on your browser settings. If you don't set the option to delete cookies with shutdown of the app, but keep cookies according to their expiration date/time. For a starter it should be ok to keep the IE browser open. It doesn't work at all, if using another browser. The OLE infrastructure of inetctls.inet, msxml2.xmlhttp or internetexplorer.application is only seeing ie sessions. Not only cookie based sessions. Basic auth, for example,  is not cookie based, but is not used with typical community website logins anyway.

@wheat01: More info is needed for more concrete code.

I just can add one more general hint: If the website supports developer add ons and offers web services or any other API you should look into that. Google, Yahoo, Twitter and many more offer oAuth authentication. I have provided a little oAuth lib here:
http://vfpoauth.codeplex.com/

Bye, Olaf.
0
 
wheat01Author Commented:
Experts

Thanks for the direction.  I finally "solved" the issue with the code below.  Not as elegant but it works every time without user intervention.

loShell=createObject("wscript.shell")
oInet=CREATEOBJECT("InternetExplorer.Application")
oInet.Navigate("https://loginpage")

do while oInet.busy
    wait "busy" window nowait timeout 1
enddo
oInet.visible = .t.
SetForegroundWindow(oInet.hwnd)
WAIT TIMEOUT 4
loshell.sendKeys("mylogin")
WAIT TIMEOUT .5
loshell.sendkeys("{TAB}")
WAIT TIMEOUT .5
loshell.sendkeys("mypassword")
WAIT TIMEOUT .5
loshell.sendkeys("{TAB}")
WAIT TIMEOUT .5
loshell.sendkeys("{ENTER}")
WAIT TIMEOUT 2
0
 
Olaf DoschkeSoftware DeveloperCommented:
Fair enough.

You could get concrete code, if you'd simply tell us, into what site you want to log in. Login forms and specifics can be very different from site to site. There are standards like BasicAuth, working with .htaccess, but no community site uses that, this is just basic directory accesss protection.

As you use inetctls.inet, that points towards FTP, but you don't make FTP requests.

some logins base on session cookies, some demand that, some have a fallback of storing session data in a backend database.

Typically you submit a html form like the user would do manually. Some sites as gmail actually hinder automatic html form submit, so I can't guarantee I'd be able to give you code. In thqat case you could use OAuth, actually.

If html form automation would work, it would most probably be along the lines of what Cyril already gave you: You fill in user and password into the DOM of a browser and call the form submit. There are more elegant ways to submit a html form than by sendkeys. you can do document.getElementById("loginform").submit();

Again:  Login forms and specifics can be very different from site to site, not only names of the html form elements may differ, they may be captchas to hinder automation.

Bye, Olaf.
0
 
wheat01Author Commented:
Solution is the specific code required to provide the connection to the site with correct login and password which then makes the site content avaialble.

 However, I did award the points between the two experts for giving me some direction in solving the problem.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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