http username password MatLab2011b

7 views (last 30 days)
Kerry
Kerry on 1 Nov 2011
Hi,
I am trying to download data from a http site which requires our username and password.
[state,HTTP]=urlread(strcat('http://',username':'password,'@www.site/')); use to work, but done not on version 2011b of matlab.
I have checked the site works on my pc and it does.
This is in a program that accesses several areas of the site to download differen data sets.
When the code is run it returns HTTP =0 therefore I know its not working.
All help is gratefully received.
Thanks,
Kerry

Accepted Answer

Kerry
Kerry on 4 Nov 2011
Here we go... urlwrite_auth
downloads the file you wanted to the location!
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!!!!
Enjoy, it took me a 3 solid days, but I got there.
Good luck, sorry it has not come out that clear here. I can email it if you want.
Kerry
  1 Comment
Jan
Jan on 4 Nov 2011
Please use a proper code formatting as explained in the "Markup help" link.

Sign in to comment.

More Answers (3)

Jan
Jan on 1 Nov 2011
Is it a typo just in the message, or did you forget the commas around the colon?
[state,HTTP]=urlread(strcat('http://', username, ':', password, '@www.site/'));
You can try to read the web pages using Java: stackoverflow: urlread with password in java

Kerry
Kerry on 2 Nov 2011
I typed it in wrong here. Its right in the code, but does not work.
It has been suggested that the new version of Matlab is safer and does not let you pass plain text passwords. Note I can't use an older version of MatLab.
I will try the java and reply on that later, thank you Jan.
Kerry
  2 Comments
Jan
Jan on 2 Nov 2011
Did you try the base64 encoding or the user/password part already? It is not an encryption, but it cares for non-ASCII characters.
Kerry
Kerry on 2 Nov 2011
Using Java works. I can now read the pages I now need to write from them. Is there are urlwrite_auth ? I am trying to rewrite the urlread_auth but am finding it difficult.
Thanks,
Kerry

Sign in to comment.


Kerry
Kerry on 2 Nov 2011
Using Java works, the urlread_auth is excellent.
I now need a urlwrite_auth
Help anyone?
Thanks,
Kerry

Categories

Find more on Programming Utilities in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!