I am having trouble making web service calls in an iOS application built with Xamarin. This EXACT same code, that builds a request and uses HTTPWebRequest GetResponse() to get the response, used to work but now it is crashing when trying to get the response. It crashes on ReadToEnd, saying that the argument cannot be null. This code has remained unchanged since it worked. I know that there IS a response with the 400 because PostMan works and I can also see a log from the backend when I'm making the call from my Xamarin app. Searching stackoverflow is revealing that some people have resolved this issue by upgrading Xamarin and that it has something to do with the mono runtime it uses. But the latest version of Xamarin is doing the same thing for me.
...
} catch(WebException ex) {
try {
if(ex.Response == null || ex.Status != WebExceptionStatus.ProtocolError) throw;
...
Stream data = ex.Response.GetResponseStream(); if(data==null) return "ERR";
StreamReader reader = new StreamReader(data); if(reader==null) return "ERR";
string responseFromServer = reader.ReadToEnd();
Expected results should be that my GetResponse does generate a WebException in the event of an HTTP 400, however there should still be a response body to read.
Here is a stacktrace of the exception:
2019-04-02 09:15:17.367099-0400 ******.iOS[41201:4512789] *****> System.Net.WebException: Value cannot be null.
Parameter name: src ---> System.ArgumentNullException: Value cannot be null.
Parameter name: src
at System.Buffer.BlockCopy (System.Array src, System.Int32 srcOffset, System.Array dst, System.Int32 dstOffset, System.Int32 count) [0x00003] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.2.1.16/src/Xamarin.iOS/mcs/class/corlib/ReferenceSources/Buffer.cs:39
at System.Net.WebResponseStream+<ProcessRead>d__49.MoveNext () [0x00082] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.2.1.16/src/Xamarin.iOS/mcs/class/System/System.Net/WebResponseStream.cs:203
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest+<RunWithTimeoutWorker>d__241`1[T].MoveNext () [0x000e8] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.2.1.16/src/Xamarin.iOS/mcs/class/System/System.Net/HttpWebRequest.cs:956
--- End of stack trace from previous location where exception was thrown ---
at System.Net.WebConnectionStream.Read (System.Byte[] buffer, System.Int32 offset, System.Int32 count) [0x00070] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.2.1.16/src/Xamarin.iOS/mcs/class/System/System.Net/WebConnectionStream.cs:136
at System.IO.Stream.ReadByte () [0x00007] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.2.1.16/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/io/stream.cs:758
at *****.API.callService (System.String endpt, System.String parm, System.Boolean isGet) [0x003e9] in /Users/****/Projects/*****/*****/API.cs:80