Click here to Skip to main content
15,910,411 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is my code

C#
string parameters = @"OrderId,OrderedDate,OrderType,ProductId,SubscriptionPeriod,SubscriptionPrice,SubscriptionVolumeCount,SubscriptionScheduleId,UserId" +
                Environment.NewLine + "1,2013-08-07 19:41:28,1,1300002727,1,800,1,961193,1";

                   // string parameters = strArray[1];
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
                    request.ContentType = "application/x-www-form-urlencoded";
                    request.Method = "POST";
                    request.KeepAlive = false;

                    byte[] bytes = Encoding.ASCII.GetBytes(parameters);
                    request.ContentLength = bytes.Length;
                    using (Stream os = request.GetRequestStream()) // this line gets the error
                    {
                        os.Write(bytes, 0, bytes.Length);
                        os.Close();
                        using (WebResponse response = request.GetResponse())
                        {
                            if (response == null)
                            {
                                Console.WriteLine("Response is null");
                            }
                            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                            {
                                string s=reader.ReadToEnd().Trim();
                                ErrorLog.WriteLog("FujiApiPost", "FujiApiPostfn", "result:" + s, string.Empty, string.Empty);

                            }
                        }
                    }


Error:-Length = 'os.Length' threw an exception of type 'System.NotSupportedException'
Posted

1 solution

Refer - HttpWebRequest.GetRequestStream Method[^].
Quote:
NotSupportedException

The request cache validator indicated that the response for this 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.


Also follow the example given in that article at the bottom. Just do exactly as it is given there.
 
Share this answer
 
Comments
Mukesh Ghosh 20-Aug-13 6:40am    
I have check that code, but didn't find any difference, both are almost same.
also for that link where to put the URL?
You have used "using" statements, but there it is simple without "using" keywords. I am not saying that it is wrong, but I am just suggesting you to give that a try to check if it is working or not.

And from the Exception details, do you get any hint or clue?
Are you using any "custom cache validator" or something?
Mukesh Ghosh 20-Aug-13 7:20am    
No, i have post my code already.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900