1. John Fauss
  2. PowerBuilder
  3. Thursday, 22 June 2023 02:12 AM UTC

Using PB 2019 R3 Build 2779

I wish to read the contents of a text file directly from the Internet:

    http://data.iana.org/TLD/tlds-alpha-by-domain.txt

This is THE list of top-level-domains (TLD's) maintained by IANA (the Internet Assigned Numbers Authority). The end goal is to populate a DataStore with this list and use the TLD's to validate user-entered email addresses.

While I certainly could view the contents in a web browser, copy/paste into a locally-saved text file for subsequent import into a DataWindow/DataStore and database table, I would prefer the application to read the contents directly without manual intervention. The above URL is not a WebAPI interface. Because this list is a simple text file, there is no HTML or JavaScript to "hook" into via the Chromium-based web browser control, nor does there seem to be "save as" capability in the Chromium-based browser control. Using the newly-released replacement Webview2 browser control is not a viable option at this time.

Any ideas are welcomed. Thanks in advance!

John

Accepted Answer
Daryl Foster Accepted Answer Pending Moderation
  1. Thursday, 22 June 2023 03:04 AM UTC
  2. PowerBuilder
  3. # Permalink

Hi John,

The easiest way would be to just use the HttpClient.  Here is an example of how you could do it.  It's just thrown together so hasn't been tested thoroughly, but it will give you an idea.

HttpClient lnv_HttpClient
string ls_url
string ls_error
string ls_data = ''
string ls_responsetext = ''
integer li_rc
integer li_response

ls_url = 'http://data.iana.org/TLD/tlds-alpha-by-domain.txt'

lnv_HttpClient = Create HttpClient

lnv_HttpClient.SetRequestHeader('Accept', '*/*')

li_rc = lnv_HttpClient.SendRequest('GET', ls_url)

if li_rc = 1 then
	li_response = lnv_HttpClient.GetResponseStatusCode()

	if li_response = 200 then
		// The response was ok, so read the filecontents into ls_data
		li_rc = lnv_HttpClient.GetResponseBody(ls_data)
		if li_rc = 1 then
			// You would probably call a function to import the data at this stage,
			// but I've got code down below to import as an example
		else
			ls_error = 'Error getting response body for OAuth.  Return Code ' + string(li_rc)
		end if
	else
		ls_responsetext = lnv_HttpClient.GetResponseStatusText()
		ls_error = 'Error getting response~r~n' + string(li_response) + ' ' + ls_responsetext
	end if
else
	ls_error = 'Error retrieving URL ' + ls_url + ' - Return Code ' + string(li_rc)
end if

destroy lnv_HttpClient


// You can do this bit however you want, but just as an example I'll import it into a dynamic datastore
// This code would most likely be in it's own function and called from above

if ls_data <> '' then
	string ls_syntax
	datastore lds_tld
	ls_syntax = SQLCA.SyntaxFromSQL('select tld = convert(char(500), null)', 'Style(Type=Tabular)', ls_error)
	
	if Len(ls_error) = 0 then
		lds_tld = create datastore
	
		lds_tld.Create(ls_syntax, ls_error)
		
		if ls_error = '' then
			li_rc = lds_tld.ImportString(Text!, ls_data)
			
			if li_rc > 0 then
				// Display a few messages to show that it worked
				MessageBox('Debug', 'Imported ' + string(li_rc) + ' rows')
				MessageBox('Debug', lds_tld.GetItemString(1, 'tld'))
				MessageBox('Debug', lds_tld.GetItemString(li_rc, 'tld'))
			else
				ls_error = 'Error importing data. Return Code ' + string(li_rc)
			end if
		end if
	end if
end if	
	
	
	
Comment
  1. John Fauss
  2. Thursday, 22 June 2023 13:49 PM UTC
Thank you, Daryl - I'll try out your helpful suggestion! I've not had occasion to use the HTTPClient object previously, so I'm looking forward to learning about it. Much appreciated!
  1. Helpful
  1. John Fauss
  2. Friday, 23 June 2023 00:18 AM UTC
@Daryl - The code you supplied works great. As I was (and still mostly continue to be) totally unfamiliar with HTTP client protocols, I had to do a little research regarding the purpose & meaning of the SetRequestHeader argument values and other related subjects. I find the PB Help topics related to the HTTPClient and RESTClient objects technically accurate as far as they go, but are also very NON-helpful as a tutorial or in learning how to use the objects to accomplish meaningful tasks. Your code greatly helped me to bridge that gap so that I could accomplish the task at hand and more importantly, explain the code to others. Thank you!
  1. Helpful 2
  1. Daryl Foster
  2. Tuesday, 27 June 2023 06:21 AM UTC
Hi John, I'm glad I could help you after all the work you do on this forum helping people out. I use the HttpClient object quite a bit, I think it was one of the best recent additions to Powerbuilder.
  1. Helpful
There are no comments made yet.


There are replies in this question but you are not allowed to view the replies from this question.
We use cookies which are necessary for the proper functioning of our websites. We also use cookies to analyze our traffic, improve your experience and provide social media features. If you continue to use this site, you consent to our use of cookies.