How can I send GET request with cookie and any headers?
3 次查看(过去 30 天)
显示 更早的评论
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);
0 个评论
回答(1 个)
Satyam
2025-4-2
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.
0 个评论
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!