urlwrite with http authentication
Mostrar comentarios más antiguos
Hi,
I got one problem , that with urlwrite function,can it work for http authentication also? I have checked early some people said: 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.
Here I found that Authentication option in urlwrite.m is ONLY available since Matlab release 2013a. You can check your matlab version by typing ver at the Matlab prompt. With the example
URL = 'http://csa.esac.esa.int/csa/aio/product-action';
fileName = tempname;
gzFileName = [fileName '.gz'];
[gzFileName,st] = urlwrite(URL,gzFileName, 'Authentication', 'Basic', ...
'Get', {'Username', '<username>', 'password', '<password>', ...
'DATASET_ID', 'C1_CP_WHI_ELECTRON_DENSITY', 'START_DATE', ...
'2011-11-10T18:00:00Z', 'END_DATE', '2011-11-10T21:00:00Z', ...
'NON_BROWSER', '1'});
gunzip(gzFileName);
fileNames = untar(fileName);
for iFile = 1:numel(fileNames)
disp(fileNames{iFile});
end
this gives me error all the time. Is this correct parameters?
Respuestas (2)
Guillaume
el 28 de En. de 2015
1 voto
I don't think it's possible using urlwrite, but the weboptions of the newish websave allows you to specify username / password.
19 comentarios
buer
el 28 de En. de 2015
Guillaume
el 28 de En. de 2015
Actually, you are right, there are authentication properties for urlwrite as well. Doesn't the server you connect to support basic authentication?
Looking at the source code of urlwrite it uses java, just like the code in the link provided by Cedric. And just like urlwrite, that code only supports basic authentication (i.e. username and password are sent in plain text, so you'd better do that over an HTTPS connection).
So, have you tried?
urlwrite('https://somewhere/on/the/web/', 'somefile.txt', 'Authentication', 'Basic', 'Username', 'someusername', 'Password', 'somepassword');
assert(exist('somefile.txt', 'file'), 'Didn't work!')
buer
el 29 de En. de 2015
Guillaume
el 29 de En. de 2015
Ok, I've justed looked at the implementation of urlwrite (in r2014b) and it is strictly a superset of the urlwrite_auth found in Cedric's link. It uses exactly the same java functions and provides more functionality (support for proxy, post variables, etc.). So, in 2014b, there's no reason to use urlread/write_auth.
That may not be the case in R2013. You'll have to find out for yourself.
edit urlwrite.m
to look at the code and compare (in 2014b, most of it is actually implemented in urlreadwrite.m.
Perhaps, what you should do is put a breakpoint at the beginning of urlwrite.m and follow what happens step by step when you call the function. That way you'll see where it fails and hopefully why.
Note that you actually get less information out of urlwrite if you request status information. status has only two values, 0 for failure, 1 for success. If you don't request a status, you instead get an exception which details the cause of the failure. So call urlwrite with only 0 or 1 output arguments.
buer
el 29 de En. de 2015
Guillaume
el 29 de En. de 2015
Well, here you go, the problem appears to have nothing to do with authentication, but is a failure to connect to the server.
You could possibly get more details about the error by putting a breakpoint on line 91 and looking at e.message when it's hit.
buer
el 29 de En. de 2015
Guillaume
el 29 de En. de 2015
As I said, when you it the breakpoint on line 91, look at e.message (don't press step). This is will give you the full text of the error as it was raised by java, before it gets translated by matlab.
Note that urlwrite uses the system proxy if one is configured. Depending on your browser, it may use something different.
buer
el 29 de En. de 2015
Guillaume
el 29 de En. de 2015
Yes, at some point in the past, it obviously didn't have authentication since somebody felt the need to write urlwrite_auth. Since you're on R2013(a|b) you're fine with that.
Do you get the same error (Unknown Host) if you try to connect without authentication or do you get an authentication error?
buer
el 2 de Feb. de 2015
Guillaume
el 2 de Feb. de 2015
I don't think it has anything to do with authentication. It's not even tried to authenticate because matlab / java couldn't find the server.
buer
el 3 de Feb. de 2015
Guillaume
el 3 de Feb. de 2015
Does
urlread('https://www.google.com')
work for you?
Can you share the url you're trying to connect to, assuming it's not on a LAN? Even if we can't authenticate, we can see if we can initiate a connection to the server.
Guillaume
el 4 de Feb. de 2015
One thing for sure, this has nothing to do with your matlab license.
Have you looked at the Web settings in matlab preferences / Web ? Are they what they should be?
If you can't connect to even google.com, then the issue is obviously nothing to do with authentication. I would suggest you start a new question trying to resolve this connection issue, because I'm afraid I can't help you there.
Guillaume
el 4 de Feb. de 2015
Just seen your edit to the question. The syntax of your urlwrite call is wrong. It should be
[gzFileName,st] = urlwrite(URL,gzFileName, 'Authentication', 'Basic', ...
'Username', '<username>', 'password', '<password>', ...
'Get', {'DATASET_ID', 'C1_CP_WHI_ELECTRON_DENSITY', 'START_DATE', ...
'2011-11-10T18:00:00Z', 'END_DATE', '2011-11-10T21:00:00Z', ...
'NON_BROWSER', '1'});
The reply I get (without the username / password)
LOGIN_REQUESTED http://csa.esac.esa.int/csa/aio/product-action?DATASET_ID=C1_CP_WHI_ELECTRON_DENSITY&START_DATE=2011-11-10T18%3A00%3A00Z&END_DATE=2011-11-10T21%3A00%3A00Z&NON_BROWSER=1
which is expected since I didn't supply the username/password
This won't solve the problem that you can't connect to anything at all, though.
1 comentario
buer
el 28 de En. de 2015
Categorías
Más información sobre Downloads en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!