Requesting an URL via XMLHTTP made easy    All kind of Tutorials

In a lot of applications its necessary to request URLs programmatically. Access an API or just request some page and process its XHTML response are one of the most common uses. Ajaxed offers you a quick and easy way to do so. Your new friends name is lib.requestURL().

Requesting an URL is as easy as the following snippet:

sub main()
  'request an URL and store its response
  r = lib.requestURL("GET", "", empty, empty).responseText
end sub

The first parameter defines the request method (post, get, put, ...), the second is the URL (which also can be a local URL when it starts with a slash) and the thirst parameter takes request parameters which are being sent with the request (e.g. using the query string if its a GET request). Last but not least the last parameter allows us to provide some options for the request (timeout, special request headers, etc.).

The method returns an object of type IServerXMLHTTPRequest. In case of any network or connection problems the return will be nothing. So a suggested real world implementation would look like the following...

sub main()
  'request as a post with a parameter "date" set to current date
  'set the timeout to 5 seconds.
  set r = lib.requestURL( _
    "POST", "", array("date", date()), _
    array("timeout", 5)
  if r is nothing then
    lib.error("Could not request URL. It did not respond.")
  elseif r.status <> 200 then
    lib.error("Request successful but response not a success.")
    'everthing fine .. do something with it
  end if 
end sub

The preceeding example did check all the problems which can occur when requesting an URL. If you implement all those failure checks then you will be on the safe side.

Because the request is processed synchronously on server side its recommended to set a timeout and implement caching if possible. Page will load as long as the request lasts.

Ok, as the last example lets write a small page which allows us to enter an URL which is then requested parsed for all email addresses found within the page.

<!--#include virtual="/ajaxed/ajaxed.asp"-->
set page = new AjaxedPage

'we are using ajaxed PageParts for that
sub pagePart_parse()
  url = page.RFT("url")
  if url = "" then lib.error("Please enter a valid URL.")
  set r = lib.requestURL("GET", url, empty, array("timeout", 5))
  if r is nothing then
    lib.error("Could not request URL (timeout, ..). Are you sure it exists?")
  elseif r.status <> 200 then
    lib.error("URL responded with an unsuccessful status: " & r.status)
    with new Regexp
      'simple email pattern
      .pattern = "[a-z0-9_.]+@[a-z0-9_.]+"
      .global = true
      .ignoreCase = true
      set mails = .execute(r.responseText)
    end with
    if mails.count = 0 then str.write("No emails found at " & url)
    for each mail in mails
      str.write(mail & "<br/>")
  end if
end sub

sub main() %>
  <form id="frm" onsubmit="ajaxed.callback('parse', 'result');return false;">
    <input type="text" name="url" value="http://www.">
    <input type="submit" value="get all emails">
    <div id="result"></div>

<% end sub %>
Run this code — requestUrl.asp

Although its a simple example it covers everthing which you need when requesting URLs. It handles all the different failures that might occur and is still more readable than the conventional way when using XMLHTTP objects directly.

Debugging: If you have logging turned on within your setup then you will see a bunch of useful information which helps you debugging your requests. Just have a look at your log ;)

ajaxed makes heavy use of requestURL() internally as well. e.g. When requesting pages on unit tests, when localizing the users country, ...

Posted in All kind of Tutorials 3193 days ago

Version 2.1.1 Released1557 days ago