Force Closing HTTP Connection

3 vues (au cours des 30 derniers jours)
Jay Anselm
Jay Anselm le 30 Août 2024
Commenté : Jay Anselm le 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 commentaires
Walter Roberson
Walter Roberson le 30 Août 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 le 31 Août 2024
I think this issue deserves an official bug/support request.

Connectez-vous pour commenter.

Réponse acceptée

Shivam
Shivam le 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 commentaire
Jay Anselm
Jay Anselm le 5 Sep 2024
Shivam,
Thank you! This made it work.
Much appreciated,
Jay

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

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

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by