I am trying to POST some data from ASP.Net application to PHP using HttpWebRequest
object. But when I try reading the Request
content using
Stream myStream = myWebReq.GetRequestStream();
I am getting an error
'responseStream.Length' threw an exception of type 'System.NotSupportedException'.
Length = 'dataStream.Length' threw an exception of type 'System.NotSupportedException'
Position = 'dataStream.Position' threw an exception of type 'System.NotSupportedException'
Here is the code
string strURL = null;
HttpWebRequest myWebReq = default(HttpWebRequest);
HttpWebResponse myWebResp = default(HttpWebResponse);
byte[] byteData = null;
StreamReader sr = default(StreamReader);
strURL = "http://people.com.pk/nppm/hrms_ppm_service.php?dump=1";
myWebReq = (HttpWebRequest)WebRequest.Create(strURL);
myWebReq.ContentType = "application/x-www-form-urlencoded; charset=utf-8";
myWebReq.Method = "POST";
Label1.Text = Newtonsoft.Json.JsonConvert.SerializeObject(batches).ToString();
byteData = UTF8Encoding.UTF8.GetBytes(Label1.Text);
myWebReq.ContentLength = byteData.Length;
myWebReq.KeepAlive = true;
if (myWebReq.Proxy != null)
{
myWebReq.Proxy.Credentials = CredentialCache.DefaultCredentials;
}
Stream myStream = myWebReq.GetRequestStream();
if (byteData.Length > 0)
{
myStream.Write(byteData, 0, byteData.Length);
myStream.Close();
}
myWebResp = (HttpWebResponse)myWebReq.GetResponse();
sr = new StreamReader(myWebResp.GetResponseStream());
string strJSON__2 = sr.ReadToEnd();
Label1.Text = strJSON__2;