0001 function [output,status] = mrg_met_urlread(urlChar,method,params)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040 if ~usejava('jvm')
0041 error(message('MATLAB:urlread:NoJvm'));
0042 end
0043
0044 import com.mathworks.mlwidgets.io.InterruptibleStreamCopier;
0045
0046
0047 com.mathworks.mlwidgets.html.HTMLPrefs.setProxySettings
0048
0049
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
0060 if nargout == 2
0061 catchErrors = true;
0062 else
0063 catchErrors = false;
0064 end
0065
0066
0067 output = '';
0068 status = 0;
0069
0070
0071
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
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
0093 urlConnection.setReadTimeout(90000);
0094
0095
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
0117 try
0118 inputStream = urlConnection.getInputStream;
0119 byteArrayOutputStream = java.io.ByteArrayOutputStream;
0120
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
0135
0136
0137 function [urlConnection,errorid,errormsg] = mrg_met_urlreadwrite(fcn,urlChar)
0138
0139
0140
0141
0142
0143
0144
0145 urlConnection = [];
0146 errorid = '';
0147 errormsg = '';
0148
0149
0150 protocol = urlChar(1:min(find(urlChar==':'))-1);
0151
0152
0153 switch protocol
0154 case 'http'
0155 try
0156 handler = sun.net.www.protocol.http.Handler;
0157 catch exception
0158 handler = [];
0159 end
0160 case 'https'
0161 try
0162 handler = sun.net.www.protocol.https.Handler;
0163 catch exception
0164 handler = [];
0165 end
0166 otherwise
0167 handler = [];
0168 end
0169
0170
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
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
0184
0185 mwtcp = com.mathworks.net.transport.MWTransportClientPropertiesFactory.create();
0186 proxy = mwtcp.getProxy();
0187
0188
0189
0190 if isempty(proxy)
0191 urlConnection = url.openConnection;
0192 else
0193 urlConnection = url.openConnection(proxy);
0194 end
0195