0

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;
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Waseem Sattar
  • 11
  • 1
  • 7
  • can you post the complete stack trace of excpetion that is in which line it threw excpetion.... – Deepak Bhatia Nov 13 '13 at 06:58
  • excpetion is here...Stream myStream = myWebReq.GetRequestStream(); Length = 'dataStream.Length' threw an exception of type 'System.NotSupportedException' Position = 'dataStream.Position' threw an exception of type 'System.NotSupportedException' – Waseem Sattar Nov 13 '13 at 07:21

1 Answers1

0

Just check your HttpWebRequest.DefaultCachePolicy property.
As the documentation suggest that HttpWebRequest.GetRequestStream method will throw NotSupportedException only when the request cache validator indicates that the response for the request can be served from the cache; however, requests that write data must not use the cache. This exception can occur if you are using a custom cache validator that is incorrectly implemented.

Deepak Bhatia
  • 6,230
  • 2
  • 24
  • 58
  • have clear the cache with following lines...HttpWebRequest.DefaultCachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore); still problem is "NotSupportedException" – Waseem Sattar Nov 13 '13 at 09:18
  • Try this, myWebReq.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.BypassCache); //RequestCacheLevel should be BypassCache – Deepak Bhatia Nov 13 '13 at 09:24
  • @devanalysts I pasted your code, this works fine on my machine – Deepak Bhatia Nov 13 '13 at 10:05
  • i have tried with 2 machines, but still Exception is "Stream.Length throws NotSupportedException". – Waseem Sattar Nov 13 '13 at 10:15
  • @devanalysts you said error is here Stream myStream = myWebReq.GetRequestStream(), but in your stack trace posted in question 'responseStream.Length' threw an exception of type 'System.NotSupportedException'????? something you are not pasting in question correctely.... – Deepak Bhatia Nov 13 '13 at 10:26