Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Matlab's urlread() function has a "params" argument, but these are CGI-style parameters that get encoded in the URL. Authentication is done with lower-level HTTP Request parameters. Urlread doesn't support these, but you can code directly against the Java URL class to use them.</p> <p>You can also use a Sun's sun.misc.BASE64Encoder class to do the Base 64 encoding programmatically. This is a nonstandard class, not part of the standard Java library, but you know that the JVM shipping with Matlab will have it, so you can get away with coding to it.</p> <p>Here's a quick hack showing it in action.</p> <pre><code>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 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload