在this question之后,关于使用Matlab访问网页上的PDF,它最初是隐藏在Javascript函数后面的。我现在有了一个URL,它允许我直接访问页面,使用Matlab webrowser对象( PDF出现在屏幕上)可以正常工作,但为了保存PDF供后续处理,我似乎需要使用Matlab urlread/urlwrite函数。但是,这些函数不提供提供身份验证凭据的方法。
如何为Matlab的urlread/urlwrite函数提供用户名/密码?
发布于 2009-08-24 16:56:45
Matlab的urlread()函数有一个"params“参数,但这些参数都是CGI样式的参数,会在URL中进行编码。身份验证使用较低级别的HTTP请求参数完成。Urlread不支持它们,但是您可以直接针对Java URL类编写代码来使用它们。
您还可以使用Sun的sun.misc.BASE64Encoder类以编程方式进行Base64编码。这是一个非标准类,不是标准Java库的一部分,但是您知道Matlab附带的JVM将包含它,因此您可以不用编写它。
这里有一个简单的技巧,展示了它的实际应用。
function [s,info] = urlread_auth(url, user, password)
%URLREAD_AUTH Like URLREAD, with basic authentication
%
% [s,info] = urlread_auth(url, user, password)
%
% Returns bytes. Convert to char if you're retrieving text.
%
% Examples:
% sampleUrl = 'http://browserspy.dk/password-ok.php';
% [s,info] = urlread_auth(sampleUrl, 'test', 'test');
% txt = char(s)
% Matlab's urlread() doesn't do HTTP Request params, so work directly with Java
jUrl = java.net.URL(url);
conn = jUrl.openConnection();
conn.setRequestProperty('Authorization', ['Basic ' base64encode([user ':' password])]);
conn.connect();
info.status = conn.getResponseCode();
info.errMsg = char(readstream(conn.getErrorStream()));
s = readstream(conn.getInputStream());
function out = base64encode(str)
% Uses Sun-specific class, but we know that is the JVM Matlab ships with
encoder = sun.misc.BASE64Encoder();
out = char(encoder.encode(java.lang.String(str).getBytes()));
%%
function out = readstream(inStream)
%READSTREAM Read all bytes from stream to uint8
try
import com.mathworks.mlwidgets.io.InterruptibleStreamCopier;
byteStream = java.io.ByteArrayOutputStream();
isc = InterruptibleStreamCopier.getInterruptibleStreamCopier();
isc.copyStream(inStream, byteStream);
inStream.close();
byteStream.close();
out = typecast(byteStream.toByteArray', 'uint8'); %'
catch err
out = []; %HACK: quash
end发布于 2016-10-17 09:01:59
作为对此的更新:另一个选项是新函数webread,您可以明确地提供用于基本身份验证的用户名和密码。
options = weboptions('Username','user','Password','your password');
data = webread(url, options);这也可以用于websave或webwrite。有关weboptions here的更多信息
发布于 2011-11-05 05:45:39
urlwrite_auth是下一步,所以这里是...
function [output,status]=urlwrite_auth(url, user, password,location,wanted)
%URLWRITE_AUTH Like URLWRITE, with basic authentication
%
% location is where you want the file saved
% wanted is the name of the file you want
% Returns the output file which is now saved to location.
%
% Examples:
% sampleUrl = 'http://browserspy.dk/password-ok.php';
% [output,status] = urlwrite_auth(sampleUrl, 'user', 'password', location, wanted);
% Matlab's urlread() doesn't do HTTP Request params, so work directly with Java
jUrl = java.net.URL(url);
conn = jUrl.openConnection();
conn.setRequestProperty('Authorization', ['Basic ' base64encode([user ':' password])]);
conn.connect()
%note this calls the function below
% Specify the full path to the file so that getAbsolutePath will work when the
% current directory is not the startup directory and urlwrite is given a
% relative path.
file = java.io.File(location);
% the path.
try
file = file.getCanonicalFile;
catch
error('MATLAB:urlwrite:InvalidOutputLocation','Could not resolve file "%s".',char(file.getAbsolutePath));
end
% Open the output file.
pathy=strcat(location,'\',wanted);
try
fileOutputStream = java.io.FileOutputStream(pathy);
catch
error('MATLAB:urlwrite:InvalidOutputLocation','Could not open output file "%s".',char(file.getAbsolutePath));
end
% Read the data from the connection.
try
inputStream = conn.getInputStream;
import com.mathworks.mlwidgets.io.InterruptibleStreamCopier;
% This StreamCopier is unsupported and may change at any time.
isc = InterruptibleStreamCopier.getInterruptibleStreamCopier;
isc.copyStream(inputStream,fileOutputStream);
inputStream.close;
fileOutputStream.close;
output = char(file.getAbsolutePath);
catch
fileOutputStream.close;
delete(file);
if catchErrors, return
else error('MATLAB:urlwrite:ConnectionFailed','Error downloading URL. Your network connection may be down or your proxy settings improperly configured.');
end
end
status = 1;
function out = base64encode(str)
% Uses Sun-specific class, but we know that is the JVM Matlab ships with
encoder = sun.misc.BASE64Encoder();
out = char(encoder.encode(java.lang.String(str).getBytes()));
%this is the bit of code that makes it connect!!!!请注意,这是由Andrew开发的从http用户名和密码站点下载文件的答案。
https://stackoverflow.com/questions/1317931
复制相似问题