Force Closing HTTP Connection

3 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2024-8-30
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 2024-8-31
I think this issue deserves an official bug/support request.

请先登录,再进行评论。

采纳的回答

Shivam
Shivam 2024-9-1
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Call Web Services from MATLAB Using HTTP 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by