
Hello Community ,
Iam tring to call a Rest API by using a CRM Plugin but I go this Error <dataStream.Position' threw an exception of type 'System.NotSupportedException>
Can anyone identify the cause of the issue please ?
Here is my Post call :
public Object POST(string url, string jsonContent, string header)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
Byte[] byteArray = encoding.GetBytes(jsonContent);
request.ContentLength = byteArray.Length;
request.ContentType = @"application/json";
using (Stream dataStream = request.GetRequestStream())
{
dataStream.Write(byteArray, 0, byteArray.Length);
try
{
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
StreamReader sr = new StreamReader(dataStream);
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
var jsonObject = serializer.DeserializeObject(sr.ReadToEnd());
return jsonObject;
}
}
catch (WebException ex)
{
// Log exception and throw as for GET example above
throw new Exception(ManageException(ex));
}
}
}
*This post is locked for comments
I have the same question (0)I correct this error by using this code :
Stream responseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(responseStream);
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
var jsonObject = serializer.DeserializeObject(myStreamReader.ReadToEnd());