I have a problem that StreamReader.ReadToEnd() sometimes throws this exception
Exception System.ArgumentOutOfRangeException - CLR_E_OUT_OF_RANGE (1)
My stream is the response of a httpwebrequest.
After a lot of test and frustration I found out that the problem occur if the length of the response from System.Net.HttpWebResponse.request.GetResponse() has the length 512 or 1024
I have created a little test program, that shows the problem.
using GHI.Pins;
using GHI.Networking;
using System.Collections;
namespace EnOcean
{
public class Application
{
public static void Main()
{
EthernetENC28J60 enc;
enc = new GHI.Networking.EthernetENC28J60(SPI.SPI_module.SPI2, G120.P1_17, G120.P0_5, G120.P1_14);
enc.Open();
enc.EnableDynamicDns();
enc.EnableDhcp();
Thread.Sleep(3000);
// Create an HTTP Web request.
System.Net.HttpWebRequest request = System.Net.HttpWebRequest.Create("http://softcontroltest.azurewebsites.net/Change/GetJsonObjXX?Length=512") as System.Net.HttpWebRequest;
// Set request.KeepAlive to use a persistent connection.
request.KeepAlive = false;
request.ContentType = "application/json";
request.Timeout = 10000;
try
{
using (var response = (System.Net.HttpWebResponse)request.GetResponse())
{
// Error
if (response.StatusCode != System.Net.HttpStatusCode.OK)
{
Debug.Print("Resonse not OK");
}
// Grab the response
else
{
Debug.Print("Content length is " + response.ContentLength);
Debug.Print("Content type is " + response.ContentType);
using (var responseStream = response.GetResponseStream())
{
if (responseStream != null)
{
using (var reader = new StreamReader(responseStream))
{
var responseValue = reader.ReadToEnd();
Debug.Print("String: " + responseValue);
}
}
}
}
}
}
catch (Exception ex)
{
}
}
}
}
In the url the Length specifies how many bytes to request.
If Length=511 everything is ok
If Length=513 everything is ok
If Length=512 it throws an exception