Force Closing HTTP Connection

3 Ansichten (letzte 30 Tage)
Jay Anselm
Jay Anselm am 30 Aug. 2024
Kommentiert: Jay Anselm am 5 Sep. 2024
I have written the function below to communicate over HTTP. It works but MATLAB appears to be holding the connection open. I have another application (non-MATLAB) that I also use to communicate separately. Once I call this function for the first time, the other application stops working until I close MATLAB. I've dug around but I can't figure out how to make MATLAB give up the connection. MATLAB states that the connection is not persistent but my experience seems to indicate otherwise. Is there a way to force a disconnect?
function response = sendRequest(URL,JSON_Message)
uri= matlab.net.URI(URL);
request=matlab.net.http.RequestMessage;
request.Method = 'POST';
request.Body =JSON_Message;
% matlab.net.http.HTTPOptions persists across requests to reuse previous
persistent options
if isempty(options)
options = matlab.net.http.HTTPOptions('Authenticate',false,'KeepAliveTimeout',0,'ConnectTimeout',10,'ResponseTimeout',60, ...
'UseProgressMonitor',false,'UseProxy',false,'VerifyServerName',false,'DataTimeout',60,'MaxRedirects',0);
end
% Send request and get response and history of transaction.
[data, ~, history] = request.send(uri, options);
response = data.Body.Data;
end
  2 Kommentare
Walter Roberson
Walter Roberson am 30 Aug. 2024
There are two possibilities here:
First off, the connection might be stuck somewhere in the MATLAB implementing code. I do not know if it is possible to get it unstuck in this case. Quite possibly not.
Secondly, the connection might be stuck in the network stack, such as if the network stack is waiting for a missing ACK. The only way to get it unstuck in this circumstance is to shoot the process -- and even that is not certain.
dpb
dpb am 31 Aug. 2024
I think this issue deserves an official bug/support request.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Shivam
Shivam am 1 Sep. 2024
Hi Jay,
I get that while establising the HTTP connection with the parameter 'KeepAliveTimeout' set to 0, the connection doesn't get closed after first connection with the server.
As a workaround, you can explicitly set the 'Connection' header to 'close'.
Please refer to the modified function to close the connection after response is sent:
function response = sendRequest(URL,JSON_Message)
%
% ..
%
% Set the Connection header to 'close' to ensure the connection is not kept alive
request.Header = matlab.net.http.field.GenericField('Connection', 'close');
%
% ...
%
% Check the Connection header in the response
connectionHeader = data.Header.getFields('Connection');
if ~isempty(connectionHeader)
disp(['Connection header in response: ', connectionHeader.Value]); % connectionHeader.Value is "close" if the connection gets closed
else
disp('No Connection header found in response.');
end
end
I hope it helps in resolving the issue.
Thanks and Regards,
Shivam
  1 Kommentar
Jay Anselm
Jay Anselm am 5 Sep. 2024
Shivam,
Thank you! This made it work.
Much appreciated,
Jay

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Call Web Services from MATLAB Using HTTP finden Sie in Help Center und File Exchange

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by