フィルターのクリア

Force Closing HTTP Connection

8 ビュー (過去 30 日間)
Jay Anselm
Jay Anselm 2024 年 8 月 30 日
コメント済み: Jay Anselm 2024 年 9 月 5 日
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
  1 件のコメント
Jay Anselm
Jay Anselm 2024 年 9 月 5 日
Shivam,
Thank you! This made it work.
Much appreciated,
Jay

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCall 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