2009-08-23 5 views
5

this question से, मैटलैब का उपयोग करके किसी वेब पेज पर पीडीएफ तक पहुंचने के संबंध में, जिसे मूल रूप से जावास्क्रिप्ट फ़ंक्शन के पीछे दफनाया जाता है। अब मेरे पास एक यूआरएल है जो मुझे सीधे पृष्ठ तक पहुंचने की इजाजत देता है, यह मैटलैब वेब्रोसर ऑब्जेक्ट (पीडीएफ स्क्रीन पर दिखाई देता है) का उपयोग करके ठीक काम करता है, लेकिन बाद में प्रसंस्करण के लिए पीडीएफ को सहेजने के लिए मुझे मैटलैब यूआरएलड/यूआरएलआरआईटी फ़ंक्शन का उपयोग करने की आवश्यकता होती है । हालांकि, ये फ़ंक्शन प्रमाणीकरण प्रमाण-पत्र प्रदान करने के लिए कोई विधि प्रदान नहीं करते हैं।मैटलैब urlread/urlwrite का उपयोग कर वेब संसाधन तक पहुंचने के लिए मैं उपयोगकर्ता नाम/पासवर्ड कैसे प्रदान करूं?

मैटलैब के urlread/urlwrite कार्यों के लिए मैं उपयोगकर्ता नाम/पासवर्ड कैसे प्रदान करूं?

उत्तर

6

मैटलैब के यूआरएलएड() फ़ंक्शन में "पैरा" तर्क है, लेकिन ये सीजीआई-शैली पैरामीटर हैं जो यूआरएल में एन्कोड किए जाते हैं। प्रमाणीकरण निम्न-स्तर 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 
+0

+1: URLREAD कमियों पर अच्छा पकड़। – gnovice

+0

नीट - मुझे एहसास नहीं हुआ था कि बेस 64 कोडिंग करने के लिए यह इतना सरल था। मुझे पकड़ने की योजना है क्योंकि मुझे नहीं लगता कि sysadmin ने मेरी अपर्याप्त पहुंच विधियों की सराहना की - अब एक पीडीएफ फ़ाइल के बजाय मुझे "ऐसा न करें" HTML पृष्ठ मिलता है! जो वास्तव में काफी उचित है, कूटनीति मोड में स्विचिंग: ओह: –

-2

मुझे मैटलैब नहीं पता, यह सिर्फ एक शिक्षित अनुमान है।

समारोह प्रलेखन here सूचियों विकल्प के रूप में इतना:

s = urlread('url','method','params') 
वे प्रमाणीकरण की किस तरह यह कर सकते हैं या काम नहीं कर सकता का उपयोग पर निर्भर करता है

, आप एक पोस्ट विधि का उपयोग करना चाहते करने जा रहे हैं।

// Params is supposed to be a "cell array of name/value pairs, I don't know matlab... 
s = urlread('http://whatever.com','post', {'username' 'ian'; 'password' 'awesomepass'}) 

आप वास्तविक अनुरोध HTML प्रपत्र को देखने या फ़ायरबग में शुद्ध टैब को देखने क्या वास्तविक नाम का/वह उपयोगकर्ता नाम और पासवर्ड पैरामीटर के मान को देखने के लिए करना होगा।

0

यह पता चला है कि इंट्रानेट साइट मूल प्रमाणीकरण का उपयोग कर रही है, जो कि मैटलैब आउट-ऑफ-द-बॉक्स द्वारा समर्थित नहीं है लेकिन गणित साइट here पर वर्णित एक समाधान समाधान है जो ठीक काम करता है। पहले उदाहरण में मैंने फायरबग का इस्तेमाल किया ताकि मुझे बेस 64 एन्कोडेड स्ट्रिंग प्राप्त हो सके जो मुझे एक्सेस के लिए जरूरी था, लेकिन मैंने उपकरण here का उपयोग करके सीधी गणना भी की। मैंने अब अपनी पीडीएफ रिपोर्ट फ़ाइल डिस्क पर सहेजी है - इसलिए काम पूरा हो गया। मेरी अगली चाल के लिए मैं इसे पाठ में परिवर्तित कर दूंगा ...

मेरी समझ यह है कि प्राप्त और पोस्ट विधियां मूल प्रमाणीकरण विधि से अलग होती हैं, लेकिन मूल प्रमाणीकरण अक्सर खुले नेट पर उपयोग नहीं किया जाता है।

+0

पर अधिक जानकारी यदि आप अपने कार्यकाल के साथ लंबे समय तक चलते हैं, तो urlread() और urlreadwrite() को एक अलग निर्देशिका में कॉपी करने और उन्हें नाम बदलने पर विचार करें। आपके मैटलैब इंस्टॉलेशन को संशोधित करना जोखिम भरा हो सकता है, भले ही यह मैथवर्क्स आपको ऐसा करने के लिए कह रहा हो, और आपको इसे अलग-अलग इंस्टॉल करने की आवश्यकता होगी। –

1

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!!!! 

नोट इस एंड्रयू द्वारा जवाब एक http यूज़रनेम और पासवर्ड साइट से फ़ाइलों को डाउनलोड करने का एक विकास है।

0

इस के लिए एक अद्यतन के रूप में: दूसरा विकल्प नया फ़ंक्शन webread है जिसे आप स्पष्ट रूप से मूल प्रमाणीकरण के लिए उपयोगकर्ता नाम और पासवर्ड दे सकते हैं।

options = weboptions('Username','user','Password','your password'); 
data = webread(url, options); 

यह भी websave या webwrite के लिए इस्तेमाल किया जा सकता है। weboptionshere