Main Content

matlab.net.http.CookieInfo.collectFromLog

Class: matlab.net.http.CookieInfo
Package: matlab.net.http

Latest CookieInfo objects from HTTP response message history

Description

example

infos = matlab.net.http.CookieInfo.collectFromLog(history) returns CookieInfo objects for all the valid cookies found in the Set-Cookie header fields of the response message history. Use collectFromLog to obtain the latest cookies from a history of messages, such as those exchanged during a transaction involving authentication or redirection. If a server sends multiple versions of the same cookie, then collectFromLog returns only the most recent. This method also eliminates cookies that might not be valid for the URI of the server, that is, whose Domain is inconsistent with the request URI.

Input Arguments

expand all

Log records, specified as a vector of matlab.net.http.LogRecord objects. The RequestMessage.send method returns a LogRecord as an optional argument.

Attributes

Sealedtrue
Statictrue

Examples

expand all

Eliminate message redirection by reusing cookies.

Send a message to a fictional website using redirection. Multiple messages are exchanged. To execute this code, you must provide a valid URI.

import matlab.net.http.*
import matlab.net.http.field.*
r = RequestMessage;
[resp,~,history] = r.send('http://www.somewebsite.com');
disp(length(history))
4

Extract the cookies from the message history.

cookieInfos = CookieInfo.collectFromLog(history);
if ~isempty(cookieInfos)
    cookies = [cookieInfos.Cookie];
end

Apply the cookies to the next request. Only one message is exchanged.

r = RequestMessage([],CookieField(cookies));
[resp,~,history] = r.send('http://www.somewebsite.com');
disp(length(history))
1
Introduced in R2016b