sending session cookie with each subsequent HTTP request in MATLAB
18 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Behzad Abdollahi
il 17 Ott 2016
Modificato: Behzad Abdollahi
il 20 Ott 2016
I am trying to communicate to a RESTFul API using MATLAB. the API uses session cookie to remember the user. My MATALB is 2015b and as long as I know, it does not support the new HTTP Interface .
my problem is that using the webread and webwrite functions in MATLAB, the session cookie is not attached to the request and therefore each time server generates a new session.
I tried to use urlread2 to read the session cookie and generate a new one with the same content, for sending back to the server, but the session cookie is HTTPOnly and therefore my generated cookie is not valid for the server.
Can anyone help me with an idea or solution? does MATLAB 2016b forward the session cookie automatically (using HTTP interface)?
2 Commenti
Kirby Fears
il 17 Ott 2016
Modificato: Kirby Fears
il 17 Ott 2016
Behzad,
Are you able to extract the "Set-Cookie" header despite this being an HTTPOnly cookie? As long as you can extract it, you should be able to send it back using the "Cookie" header.
The full cookie should look something like this:
'SessionId=123abc; path=/; HttpOnly'
I've sent cookies back successfully in Matlab 2015a using this syntax:
opts = weboptions('KeyName','Cookie','KeyValue',myCookie);
response = webread(addr2,opts);
Hope this helps. Sorry I don't have Matlab 2016b to test out the new HTTP API.
Bill Crum
il 18 Ott 2016
I've just - this morning! - had some success with the following in MATLAB 2016b. I don't know if this works or not in earlier versions. Hope this is helpful but happy for a critique as I am a noob at this stuff.
% Set the first-time login credentials
obj.options = weboptions('Username', obj.username, 'Password', obj.password, 'RequestMethod', 'post');
% Get the JSESSIONID
% Note that /data/JSESSION is appropriate for XNAT but
% will likely be different for your application
obj.jsessionid = webread(strcat(obj.server, '/data/JSESSION'), obj.options);
% Set future authenticated login options
obj.loginoptions = weboptions('KeyName', 'cookie', 'KeyValue', ['JSESSIONID=', obj.jsessionid]);
...
% Time passes ...
% Read some data
rlist = webread(strcat(obj.server, restpath), obj.loginoptions);
Risposta accettata
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Web Services in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!