-
Notifications
You must be signed in to change notification settings - Fork 253
/
Copy pathplugin_urlreadwrite.m
99 lines (89 loc) · 3.11 KB
/
plugin_urlreadwrite.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
function [urlConnection,errorid,errormsg] = plugin_urlreadwrite(fcn,urlChar)
%URLREADWRITE A helper function for URLREAD and URLWRITE.
% Adapted by A. Delorme from
% Matthew J. Simoneau, June 2005
% Copyright 1984-2011 The MathWorks, Inc.
% $Revision: 1.1.6.7 $ $Date: 2011/10/22 22:05:21 $
% Default output arguments.
urlConnection = [];
errorid = '';
errormsg = '';
% Determine the protocol (before the ":").
protocol = urlChar(1:min(find(urlChar==':'))-1);
% Try to use the native handler, not the ice.* classes.
switch protocol
case 'http'
try
handler = sun.net.www.protocol.http.Handler;
catch exception %#ok
handler = [];
end
case 'https'
try
handler = sun.net.www.protocol.https.Handler;
catch exception %#ok
handler = [];
end
otherwise
handler = [];
end
%Try to fix proxy
useNewProxyInfo = 0;
try
if exist('OCTAVE_VERSION', 'builtin') == 0
[matlabversion matlabdate] = version;
matlabversion2 = regexp(matlabversion,'R20(\d\d)([abcd])','match');
if ~isempty(matlabversion2)
matlabyear = regexp(matlabversion2,'20(\d\d)','match');
if ~isempty(matlabyear)
matlabyear = str2double(matlabyear{1,1});
%matlabsubversion = regexp(matlabversion2,strcat(num2str(matlabyear), "([abcd])"),'match','once');
useNewProxyInfo = matlabyear>=2018;
end
end
end
catch
warning('An error occurred when checking MATLAB/Octave version');
end
try
if useNewProxyInfo == 1
s = settings;
proxyHost = s.matlab.web.ProxyHost.ActiveValue;
proxyPort = s.matlab.web.ProxyPort.ActiveValue;
switch protocol
case 'http'
java.lang.System.setProperty('http.proxyHost',proxyHost);
java.lang.System.setProperty('http.proxyPort',proxyPort);
case 'https'
s = settings;
proxyHost = s.matlab.web.ProxyHost.ActiveValue;
proxyPort = s.matlab.web.ProxyPort.ActiveValue;
java.lang.System.setProperty('https.proxyHost',proxyHost);
java.lang.System.setProperty('https.proxyPort',proxyPort);
otherwise
warning('Unknown web protocol');
end
end
catch
warning('An error occurred when checking proxy information using new format');
end
% Create the URL object.
try
if isempty(handler)
url = java.net.URL(urlChar);
% url = javaObject('java.net.URL', urlChar); % Octave
else
url = java.net.URL([],urlChar,handler);
% url = javaObject('java.net.URL', [],urlChar,handler); % Octave
end
catch exception %#ok
errorid = ['MATLAB:' fcn ':InvalidUrl'];
errormsg = 'Either this URL could not be parsed or the protocol is not supported.';
return
end
% Open a connection to the URL.
urlConnection = url.openConnection;
% build up the MATLAB User Agent
mlUserAgent = ['MATLAB R' version('-release') ' ' version('-description')];
% set User-Agent
urlConnection.setRequestProperty('User-Agent', mlUserAgent);