Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

I Have to post data to Odata webservice. In this post I need to pass token header also. I got token value now I have to try to pass Token Header value and Data also but when i do this I'm getting this kind of ERROR: Stream was not readable

  try

            {

           

                byte[] auth = Encoding.UTF8.GetBytes("mobtest" + ":" + "welcome1");

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://Server:8000/sap/opu/odata/sap/ZCUST_TESTING_SRV/post_cust");

                //byte[] byteArray = Encoding.UTF8.GetBytes(postXml);

                byte[] byteArray = Encoding.ASCII.GetBytes(jsonnew);

                request.Credentials = new NetworkCredential("mobtest","welcome1");

                request.ContentType = "application/atom+xml";

                request.Method = "GET";

                //request.Headers["Authorization"] = "Basic " + System.Convert.ToBase64String(auth);

                request.Headers["X-CSRF-Token"] = "Fetch";

                HttpWebResponse responseH = (HttpWebResponse)request.GetResponse();

                xToken = responseH.Headers["X-CSRF-Token"];

                Console.WriteLine("xToken : " + xToken.ToString());

                responseH.Close();

                try

                {

                    HttpWebRequest request_post = (HttpWebRequest)WebRequest.Create("http://Server:8000/sap/opu/odata/sap/ZCUST_TESTING_SRV/post_cust");

                    request_post.Credentials = new NetworkCredential("mobtest", "welcome1");

                    request_post.Method = "POST";

                    request_post.Headers["X-CSRF-Token"] = xToken;

                   

                    Console.WriteLine("xToken POST : " + xToken.ToString());

                    request_post.ContentType = "application/json";

                    request_post.Accept = "application/json";

                    request_post.ContentLength = byteArray.Length;

               

                    Stream dataStream1 = request_post.GetRequestStream();

                    dataStream1.Write(byteArray, 0, byteArray.Length);

                    Console.WriteLine("byteArray : "+byteArray);

                    dataStream1.Close();

                    Console.WriteLine("datastream1");

                    HttpWebResponse response1 = (HttpWebResponse)request.GetResponse();

                    Console.WriteLine("Post response : " + response1.ToString());

                    dataStream1 = response1.GetResponseStream();

                  

                    StreamReader reader1 = new StreamReader(dataStream1);

                    Console.WriteLine("  Console.WriteLine(reader1 : " + reader1);

                    string responseFromServer1 = reader1.ReadToEnd();

                    Console.WriteLine("  Console.WriteLine(responseFromServer1 : " + responseFromServer1);

                    Console.Read();

                    reader1.Close();

                    dataStream1.Close();

                    response1.Close();

                 

                }

                catch(Exception exp)

                {

                    MessageBox.Show("Post Error : "+exp.Message);

                }

            }

            catch(Exception ex)

            {

                MessageBox.Show("Error : "+ex.Message);

            }

Tags:
Former Member
Not what you were looking for? View more on this topic or Ask a question