MATLAB: Can I perform basic authentication with URLREAD in MATLAB


I am using the URLREAD function to import a web page into MATLAB. However, to access the website I need to log on using basic authentication.

Best Answer

This functionality has been incorporated in Release 2013a (R2013a). See the R2013a documentation for URLREAD for more information. For previous product releases, read below for any possible workarounds:
The ability to use basic authentication with the URLREAD command is not available in MATLAB.
To work around this issue you can modify the URLREAD command to perform basic authentication. Basic Authentication is a specific authorization mechanism for web servers which is not secure. Not all web servers allow this type of authentication, so this work around may not apply to you.
The following work around has not been tested and therefore may require modification to get them to work in your situation. This implements a hard coded username and passwords into a copy of the URLREAD function. You will need to understand the basic authentication scheme in order to apply this workaround. References for more information are listed below.
1. Save a copy of the file $MATLABROOT\toolbox\matlab\iofun\urlread.m as $MATLABROOT\toolbox\matlab\iofun\urlread_basicauth.m (where $MATLABROOT is the MATLAB root directory on your machine, as returned by typing:
at the MATLAB command prompt.)
2. Open the file urlread_basicauth and add a line to add the basic authentication property to the URL connection. Around line 68 you should see the following section of code
[urlConnection,errorid,errormsg] = urlreadwrite(mfilename,urlChar);
if isempty(urlConnection)
if catchErrors, return
else error(errorid,errormsg);
Imediately after this add the following code and save the changes.
urlConnection.setRequestProperty( ...
'Authorization','Basic YWRtaW46YWRtaW4=');
3. Execute the following command at the MATLAB command prompt
rehash toolboxcache
4. Determine the encoded user name and password required for your authentication. Then subsitute your encoded username and password for "YWRtaW46YWRtaW4=" in code added in step 2. The encoding scheme is explained here: (following the link to "base-64" for more details)