Force Closing HTTP Connection

조회 수: 4 (최근 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 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