Is it possible to download a txt file from a website via lisp?

Started by Hypersonic, March 29, 2010, 05:24:10 PM

Previous topic - Next topic

Hypersonic

Is it possibly with autolisp via vl-copy (or other means) to download, or
copy a file off a website i.e. to get some form data from a text file?
An example of the path where it is at (not functional , just an example)

http://stansspecialwebpage/stationformdata/formdata_bob.txt

i.e. I want to end up with the file formdata_bob.txt on my local hard
drive....

Thanks!

BazzaCAD

Try this:

Code (autolisp) Select

(defun GetFromWeb (strUrl / webObj stat res errobj)
  (vl-load-com)
  ;; Set up variables
  (setq webObj nil stat nil res nil)
  ;; Create a new reference to the WinHttp object
  (setq webObj (vlax-invoke-method (vlax-get-acad-object) 'GetInterfaceObject "WinHttp.WinHttpRequest.5.1"))
  ;; Fetch web page
  (vlax-invoke-method webObj 'Open "GET" strUrl :vlax-false)
  (setq errobj (vl-catch-all-apply 'vlax-invoke-method (list webObj 'Send)))
  (if (null (vl-catch-all-error-p errobj))
    (progn
      (setq stat (vlax-get-property webObj 'Status))
      (if (= stat 200)
        (progn
          (setq res (vlax-get-property webObj 'ResponseText));_ Return the response value
        )
        (princ (strcat "\n!!! WEB server error: " (vlax-get-property webObj 'StatusText) "!!!"))
      )
    );_ progn
    (princ (strcat "\n!!! WEB server error:\n" (vl-catch-all-error-message errobj)))
  )
  res 
)

(GetFromWeb "http://stansspecialwebpage/stationformdata/formdata_bob.txt")


Of course it fails since the file doesn't exist...
But this does works:
Code (autolisp) Select
(GetFromWeb "http://www.opendcl.com/version/version.txt")



a.k.a.
Barry Ralphs
barryDOTralphsATgmailDOTcom

Hypersonic

"5.1.2.3" awesome!

Is there a similar method to "post" to a url??

Thanks  ;D

BazzaCAD

This code should work both ways...
We use this some GetFromWeb function to pass arguments to .PHP files, then the .PHP code writes to the database...
I'm not sure how you'd write to a .TXT file, but I'm sure .PHP could handle it, it's just over my head.
a.k.a.
Barry Ralphs
barryDOTralphsATgmailDOTcom

Fred Tomke

Fred Tomke
Dipl.-Ing. (FH) Landespflege

[ landscaper - landscape developer - digital landscape and urban design]

Danner

Wow! Thanks for the code Barry and thanks Hypersonic for suggesting.

Hypersonic

Barry, any chance you could show an example of writing to the web server url? 

Thanks, this stuff is way beyond me......need all the coaching I can get!

BazzaCAD

OK to write to our in-house database, I do this:

Code (autolisp) Select

  (setq webObj nil strUrl (strcat "http://www/officedb/common/cadEvent.php?action=add&cadID=" TMaCADProjDBNum "&user=" Draftername "&event=" Event "&dwg=" dwg))
  (setq webObj (vlax-invoke-method (vlax-get-acad-object) 'GetInterfaceObject "WinHttp.WinHttpRequest.5.1"))
  (vlax-invoke-method webObj 'Open "GET" strUrl :vlax-true)
  (vlax-invoke-method webObj 'Send)


Then the "cadEvent.php" file takes over from there & parses the arguments from the URL & posts them to the database.
I can't post "cadEvent.php" as it's pretty complicated & only writes to a mySQL database, so that doesn't sound like what you're trying to do. FYI, this is our "Big Brother" code that logs who's working on what (shown below)...

This link shows how to get PHP to write to a text file on your web server (assuming you have a web server with PHP running on it):
http://www.tizag.com/phpT/filewrite.php
So maybe that will help.


a.k.a.
Barry Ralphs
barryDOTralphsATgmailDOTcom

Hypersonic

Thanks, and I think we have been able to use the php to write to the text file, and then with a program similar to the one you posted, we were able to get the file to download to my local pc hard drive.

The part I am stuck on now is how to use autolisp to send a text file back to the web server so the web program can then pull input from that.... (at least I think that is how it should work?)

Thanks!

Fred Tomke

Sound's interesting Barry. I solve this using ADODB.Connection object using the ODBC Driver MyODBC. Then I can handle it like an Access Database like calling SQL-statements and so on. But accessing MySQL using server hosted PHP-file is a great deal.

Thanks Barry.

Fred
Fred Tomke
Dipl.-Ing. (FH) Landespflege

[ landscaper - landscape developer - digital landscape and urban design]

BazzaCAD

The ADODB connection stuff always seamed like a pain to work with....
This is a pretty easy solution, in my opinion.
a.k.a.
Barry Ralphs
barryDOTralphsATgmailDOTcom

Kerry

Quote from: Fred Tomke on March 31, 2010, 11:22:47 AM
Sound's interesting Barry. I solve this using ADODB.Connection object using the ODBC Driver MyODBC. Then I can handle it like an Access Database like calling SQL-statements and so on. But accessing MySQL using server hosted PHP-file is a great deal.

Thanks Barry.

Fred

Fred, Are you able to do that with a 64bit system ?
Perfection is not optional.
My other home is TheSwamp

Fred Tomke

Quote from: Kerry Brown on April 01, 2010, 12:02:33 AM
Fred, Are you able to do that with a 64bit system ?

Yes we are able to have access to Access databases and MySQL databases from Win7 x64.

Using ODBC to get access to MySQL you'll need to configure the database with the ODBC x64 manager of Win7 x64.
For Access you need to install the Access Database Engine 2010 x64.
Works very well for us.

That awakes my bad conscience again that I wanted to built a database access sample to show the interaction of OpenDCL, Drag&Drop and databases... I'll need to invent the 48-hour-day...  :-\

Fred
Fred Tomke
Dipl.-Ing. (FH) Landespflege

[ landscaper - landscape developer - digital landscape and urban design]

Kerry

Perfection is not optional.
My other home is TheSwamp

Fred Tomke

Yes. Note that it is needed to uninstall an existing 32-Bit Office before installiing the ADE. I know it's a mess, but it's also the only chance to solve. In the end you will have the chance again to connect Access databases in AutoCAD Map via FDO again.  :)

Using the ADE you don't connect the Jet provider anymore.
http://www.altova.com/Access-Database-OLEDB-32bit-64bit.html

Sample for creating an MS Access database file:

Code (autolisp) Select
(defun DB_ACCESS_CREATE (strFile / strExistFile oAdox sConn odbv)
  (cond
    ((not (DB_INIT_ADO)) nil)
    ((and (setq strExistFile (findfile strFile)) (not (vl-file-delete strExistFile))) nil)
    (T (setq oAdox (vlax-get-or-create-object "ADOX.Catalog"))
     (setq sConn (strcat "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" strFile ";"))
     (if (vl-catch-all-error-p (vl-catch-all-apply 'vlax-invoke-method (list oAdox 'Create sConn)))
       (progn
(vlax-release-object oAdox)
(setq oAdox nil)
       ); progn
       (progn
(setq odbv (vlax-get oAdox 'ActiveConnection))
(vlax-release-object oAdox)
       ); progn
     )); if
  ); cond

  odbv
); DB_ACCESS_CREATE


For x86 this Provider works as well - even without the Access Database Engine for Office 2010 x64.

Fred
Fred Tomke
Dipl.-Ing. (FH) Landespflege

[ landscaper - landscape developer - digital landscape and urban design]