Home > mrg > MRG_metstation > mrg_met_urlread.m

mrg_met_urlread

PURPOSE ^

A drop-in replacement for urlread in the default MATLAB insallation.

SYNOPSIS ^

function [output,status] = mrg_met_urlread(urlChar,method,params)

DESCRIPTION ^

 A drop-in replacement for urlread in the default MATLAB insallation. 

 This version adds a 90 second time out so that the calling function
 doesn't hang when the website is unreachable. It includes a version of
 urlreadwrite (a private function), copied verbatum but renamed as
 mrg_met_urlreadwrite.

 v 1.0 24/01/2012 DP
       First version
 v 1.1 August 2013 DP
       Replaced urlreadwrite calls with mrg_met_urlreadwrite which is now
       embedded at the end of this file

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [output,status] = mrg_met_urlread(urlChar,method,params)
0002 % A drop-in replacement for urlread in the default MATLAB insallation.
0003 %
0004 % This version adds a 90 second time out so that the calling function
0005 % doesn't hang when the website is unreachable. It includes a version of
0006 % urlreadwrite (a private function), copied verbatum but renamed as
0007 % mrg_met_urlreadwrite.
0008 %
0009 % v 1.0 24/01/2012 DP
0010 %       First version
0011 % v 1.1 August 2013 DP
0012 %       Replaced urlreadwrite calls with mrg_met_urlreadwrite which is now
0013 %       embedded at the end of this file
0014 
0015 %URLREAD Returns the contents of a URL as a string.
0016 %   S = URLREAD('URL') reads the content at a URL into a string, S.  If the
0017 %   server returns binary data, the string will contain garbage.
0018 %
0019 %   S = URLREAD('URL','method',PARAMS) passes information to the server as
0020 %   part of the request.  The 'method' can be 'get', or 'post' and PARAMS is a
0021 %   cell array of param/value pairs.
0022 %
0023 %   [S,STATUS] = URLREAD(...) catches any errors and returns 1 if the file
0024 %   downloaded successfully and 0 otherwise.
0025 %
0026 %   Examples:
0027 %   s = urlread('http://www.mathworks.com')
0028 %   s = urlread('ftp://ftp.mathworks.com/README')
0029 %   s = urlread(['file:///' fullfile(prefdir,'history.m')])
0030 %
0031 %   From behind a firewall, use the Preferences to set your proxy server.
0032 %
0033 %   See also URLWRITE.
0034 
0035 %   Matthew J. Simoneau, 13-Nov-2001
0036 %   Copyright 1984-2008 The MathWorks, Inc.
0037 %   $Revision: 1.3.2.11 $ $Date: 2010/08/23 23:10:47 $
0038 
0039 % This function requires Java.
0040 if ~usejava('jvm')
0041    error(message('MATLAB:urlread:NoJvm'));
0042 end
0043 
0044 import com.mathworks.mlwidgets.io.InterruptibleStreamCopier;
0045 
0046 % Be sure the proxy settings are set.
0047 com.mathworks.mlwidgets.html.HTMLPrefs.setProxySettings
0048 
0049 % Check number of inputs and outputs.
0050 error(nargchk(1,3,nargin))
0051 error(nargoutchk(0,2,nargout))
0052 if ~ischar(urlChar)
0053     error('MATLAB:urlread:InvalidInput','The first input, the URL, must be a character array.');
0054 end
0055 if (nargin > 1) && ~strcmpi(method,'get') && ~strcmpi(method,'post')
0056     error('MATLAB:urlread:InvalidInput','Second argument must be either "get" or "post".');
0057 end
0058 
0059 % Do we want to throw errors or catch them?
0060 if nargout == 2
0061     catchErrors = true;
0062 else
0063     catchErrors = false;
0064 end
0065 
0066 % Set default outputs.
0067 output = '';
0068 status = 0;
0069 
0070 %%
0071 % GET method.  Tack param/value to end of URL.
0072 if (nargin > 1) && strcmpi(method,'get')
0073     if mod(length(params),2) == 1
0074         error('MATLAB:urlread:InvalidInput','Invalid parameter/value pair arguments.');
0075     end
0076     for i=1:2:length(params)
0077         if (i == 1), separator = '?'; else separator = '&'; end
0078         param = char(java.net.URLEncoder.encode(params{i}));
0079         value = char(java.net.URLEncoder.encode(params{i+1}));
0080         urlChar = [urlChar separator param '=' value];
0081     end
0082 end
0083 
0084 % Create a urlConnection.
0085 [urlConnection,errorid,errormsg] = mrg_met_urlreadwrite(mfilename,urlChar);
0086 if isempty(urlConnection)
0087     if catchErrors, return
0088     else error(errorid,errormsg);
0089     end
0090 end
0091 
0092 %% The timeout (in milliseconds)
0093 urlConnection.setReadTimeout(90000);
0094 
0095 % POST method.  Write param/values to server.
0096 if (nargin > 1) && strcmpi(method,'post')
0097     try
0098         urlConnection.setDoOutput(true);
0099         urlConnection.setRequestProperty( ...
0100             'Content-Type','application/x-www-form-urlencoded');
0101         printStream = java.io.PrintStream(urlConnection.getOutputStream);
0102         for i=1:2:length(params)
0103             if (i > 1), printStream.print('&'); end
0104             param = char(java.net.URLEncoder.encode(params{i}));
0105             value = char(java.net.URLEncoder.encode(params{i+1}));
0106             printStream.print([param '=' value]);
0107         end
0108         printStream.close;
0109     catch
0110         if catchErrors, return
0111         else error('MATLAB:urlread:ConnectionFailed','Could not POST to URL.');
0112         end
0113     end
0114 end
0115 
0116 % Read the data from the connection.
0117 try
0118     inputStream = urlConnection.getInputStream;
0119     byteArrayOutputStream = java.io.ByteArrayOutputStream;
0120     % This StreamCopier is unsupported and may change at any time.
0121     isc = InterruptibleStreamCopier.getInterruptibleStreamCopier;
0122     isc.copyStream(inputStream,byteArrayOutputStream);
0123     inputStream.close;
0124     byteArrayOutputStream.close;
0125     output = native2unicode(typecast(byteArrayOutputStream.toByteArray','uint8'),'UTF-8');
0126 catch
0127     if catchErrors, return
0128     else error('MATLAB:urlread:ConnectionFailed','Error downloading URL. Your network connection may be down or your proxy settings improperly configured.');
0129     end
0130 end
0131 
0132 status = 1;
0133 
0134 %% Auxlillary function
0135 % Copied verbatim (but renamed) from urlreadwrite in:
0136 % C:\MATLAB\R2011a\toolbox\matlab\iofun\private\urlreadwrite.m
0137 function [urlConnection,errorid,errormsg] = mrg_met_urlreadwrite(fcn,urlChar)
0138 %URLREADWRITE A helper function for URLREAD and URLWRITE.
0139 
0140 %   Matthew J. Simoneau, June 2005
0141 %   Copyright 1984-2009 The MathWorks, Inc.
0142 %   $Revision: 1.1.6.5 $ $Date: 2009/09/28 20:28:02 $
0143 
0144 % Default output arguments.
0145 urlConnection = [];
0146 errorid = '';
0147 errormsg = '';
0148 
0149 % Determine the protocol (before the ":").
0150 protocol = urlChar(1:min(find(urlChar==':'))-1);
0151 
0152 % Try to use the native handler, not the ice.* classes.
0153 switch protocol
0154     case 'http'
0155         try
0156             handler = sun.net.www.protocol.http.Handler;
0157         catch exception %#ok
0158             handler = [];
0159         end
0160     case 'https'
0161         try
0162             handler = sun.net.www.protocol.https.Handler;
0163         catch exception %#ok
0164             handler = [];
0165         end
0166     otherwise
0167         handler = [];
0168 end
0169 
0170 % Create the URL object.
0171 try
0172     if isempty(handler)
0173         url = java.net.URL(urlChar);
0174     else
0175         url = java.net.URL([],urlChar,handler);
0176     end
0177 catch exception %#ok
0178     errorid = ['MATLAB:' fcn ':InvalidUrl'];
0179     errormsg = 'Either this URL could not be parsed or the protocol is not supported.';
0180     return
0181 end
0182 
0183 % Get the proxy information using MathWorks facilities for unified proxy
0184 % preference settings.
0185 mwtcp = com.mathworks.net.transport.MWTransportClientPropertiesFactory.create();
0186 proxy = mwtcp.getProxy(); 
0187 
0188 
0189 % Open a connection to the URL.
0190 if isempty(proxy)
0191     urlConnection = url.openConnection;
0192 else
0193     urlConnection = url.openConnection(proxy);
0194 end
0195

Generated on Thu 29-May-2014 21:29:53 by m2html © 2005