How can I send GET request with cookie and any headers?

3 vues (au cours des 30 derniers jours)
shayan joyboy
shayan joyboy le 25 Sep 2020
How can I send this request by Matlab ?
URL : Example.com
Host: ExampleSite.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
X-Requested-With: XMLHttpRequest
Referer: ExampleReferer.com
Connection: keep-alive
Cookie: myCookie
MATLAB has it's own header fields and I can't define headers like Accept-Encoding or Cookie or Accept ...
I tried key-Value pairs but it's useful.
Cookie = 'myCookie';
options = weboptions('Timeout', 60, 'UserAgent','Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0',...
'KeyName','X-Requested-With','KeyValue','XMLHttpRequest',...
'KeyName','Accept','KeyValue','*/*', ...
'KeyName','Cache-Control','KeyValue','no-cache', ...
'KeyName','Accept-Encoding','KeyValue','gzip, deflate, br', ...
'KeyName','Connection','KeyValue','keep-alive', ...
'KeyName','Accept-Language','KeyValue','en-US,en;q=0.5', ...
'KeyName','Referer','KeyValue','ExampleReferer.com', ...
'KeyName','TE','KeyValue','Tailers', ...
'KeyName','Host','KeyValue','ExampleSite.com', ...
'KeyName','Cookie','KeyValue',Cookie);
url = "Example.com";
resp = webread(url,options);

Réponses (1)

Satyam
Satyam le 2 Avr 2025
Hi Shayan,
I understand that you want to include HTTP headers and cookies in your GET request. You can leverage “webread” function to send a GET request. Refer to the documentation of "webread" to understand how you can do this. https://www.mathworks.com/help/matlab/ref/webread.html
To include headers, you can pass a “weboptions” object to the “webread” function and specify your headers and cookie in “HeaderFields” argument. Refer to the documentation to get accustomed to the “HeaderFields” property. https://www.mathworks.com/help/matlab/ref/weboptions.html#d126e1978600
Here is a sample code snippet which implements the above approach:
% Define the URL and headers
url = 'https://example.com';
% Create web options with the required headers
options = weboptions(...
'HeaderFields', { ...
'User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0'; ...
'Accept', '*/*'; ...
'Accept-Language', 'en-US,en;q=0.5'; ...
'Accept-Encoding', 'gzip, deflate'; ...
'X-Requested-With', 'XMLHttpRequest'; ...
'Referer', 'ExampleReferer.com'; ...
'Connection', 'keep-alive'; ...
'Cookie', 'myCookie' ...
}, ...
'Timeout', 30); % Set a timeout period (in seconds)
% Send the GET request
response = webread(url,options);
% Display the response
disp(response);
I hope it solves your query.

Catégories

En savoir plus sur Call Web Services from MATLAB Using HTTP dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by