Webread certificate problems - roll your own java works, webread doesn't. Why?

62 ビュー (過去 30 日間)
Simon Parten
Simon Parten 2018 年 1 月 9 日
コメント済み: Adam Schroeder 2022 年 3 月 14 日
Could someone explain to me why this would fail with an obscure message
webread('http://website/', 'Authorization', ['Basic ' matlab.net.base64encode(['me:secr3t'])])
The reason is "error:14090086:SSL routines:ssl3_get_server_certificate:certificate
verify failed". Check your certificate file (C:\Program Files\MATLAB\R2018a\sys\certificates\ca\rootcerts.pem) for expired, missing or invalid certificates.
Whereas this (lightly modified and shamelessly stolen from stack overflow) appears to have no problems. Is webread doing something kooky that causes it to barf on the proxy server, where java is not? Why does matlab have certificate problems?
urlread_auth('https://website/','me','secr3t' )
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 ' matlab.net.base64encode([user ':' password])]);
conn.connect();
info.status = conn.getResponseCode();
info.errMsg = char(readstream(conn.getErrorStream()));
s = readstream(conn.getInputStream());
end
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
end
  2 件のコメント
Jörgen
Jörgen 2018 年 1 月 26 日
I solved it by setting the options=weboptions; options.CertificateFilename=(''); webread(URL,options);
Paola Arce
Paola Arce 2018 年 8 月 20 日
thanks! It worked for me

サインインしてコメントする。

採用された回答

Simon Parten
Simon Parten 2018 年 1 月 26 日
編集済み: Walter Roberson 2019 年 3 月 2 日
""" I solved it by setting the options=weboptions; options.CertificateFilename=(''); webread(URL,options); """
I also then needed to set the RequestMethod as POST, and crucially, use the 'webwrite' function, as opposed to the 'webread' function. Suddenly, things burst into life ...

その他の回答 (1 件)

Darshan Ramakant Bhat
Darshan Ramakant Bhat 2018 年 1 月 17 日
There is a detailed answer available for the similar issue in the below answer link:
I hope this will help you.
  1 件のコメント
Adam Schroeder
Adam Schroeder 2022 年 3 月 14 日
It looks like this link is no longer active. I was able to work through this successfully using Simon's accepted answer and Jörgen's comment.
options=weboptions;
options.CertificateFilename=('');
webread(URL,options);

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeWeb Services についてさらに検索

タグ

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by