Here's my answer to this problem, although it might have some bugs ...
getHeaderField was needed, note getHeaderField(0) is the response like 400 or 401
In addition, I added getErrorStream, which the server I am working with sends when there is an error
I still don't like the stream copier ...
allHeaders = struct('Response',char(urlConnection.getHeaderField(0)));
done = false;
headerIndex = 0;
while ~done
headerIndex = headerIndex + 1;
headerValue = char(urlConnection.getHeaderField(headerIndex));
if ~isempty(headerValue)
headerName = char(urlConnection.getHeaderFieldKey(headerIndex));
headerName(headerName == '-') = '_';
allHeaders(1).(headerName) = headerValue;
else
done = true;
end
end
status = struct('value',urlConnection.getResponseCode(),...
'msg',char(urlConnection.getResponseMessage));
isGood = true;
try
inputStream = urlConnection.getInputStream;
catch ME
try
inputStream = urlConnection.getErrorStream;
catch ME %#ok<*NASGU>
isGood = false;
end
end
if isGood
byteArrayOutputStream = java.io.ByteArrayOutputStream;
% This StreamCopier is unsupported and may change at any time. OH GREAT :/
isc = InterruptibleStreamCopier.getInterruptibleStreamCopier;
isc.copyStream(inputStream,byteArrayOutputStream);
inputStream.close;
byteArrayOutputStream.close;
output = native2unicode(typecast(byteArrayOutputStream.toByteArray','uint8'),'UTF-8');
else
output = '';
end