मैटलैब के यूआरएलएड() फ़ंक्शन में "पैरा" तर्क है, लेकिन ये सीजीआई-शैली पैरामीटर हैं जो यूआरएल में एन्कोड किए जाते हैं। प्रमाणीकरण निम्न-स्तर HTTP अनुरोध पैरामीटर के साथ किया जाता है। Urlread इन का समर्थन नहीं करता है, लेकिन आप जावा यूआरएल वर्ग के खिलाफ सीधे उनका उपयोग करने के लिए कोड कर सकते हैं।
आप बेस 64 एन्कोडिंग प्रोग्रामेटिक रूप से करने के लिए सूर्य के sun.misc.BASE64Encoder क्लास का भी उपयोग कर सकते हैं। यह एक गैर मानक वर्ग है, मानक जावा लाइब्रेरी का हिस्सा नहीं है, लेकिन आप जानते हैं कि मैटलैब के साथ जेवीएम शिपिंग में यह होगा, इसलिए आप इसे कोडिंग से दूर कर सकते हैं।
यहां एक त्वरित हैक इसे क्रिया में दिखा रहा है।
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
स्रोत
2009-08-24 16:56:44
+1: URLREAD कमियों पर अच्छा पकड़। – gnovice
नीट - मुझे एहसास नहीं हुआ था कि बेस 64 कोडिंग करने के लिए यह इतना सरल था। मुझे पकड़ने की योजना है क्योंकि मुझे नहीं लगता कि sysadmin ने मेरी अपर्याप्त पहुंच विधियों की सराहना की - अब एक पीडीएफ फ़ाइल के बजाय मुझे "ऐसा न करें" HTML पृष्ठ मिलता है! जो वास्तव में काफी उचित है, कूटनीति मोड में स्विचिंग: ओह: –