Click here to Skip to main content
15,860,844 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi in the below code I am receiving error
C#
"stream.length' threw an exception of type 'system.notsupportedexception'"

I have tried using MemoryStream but still its the same??
What i am missing????
C#
protected void Button1_Click(object sender, EventArgs e)
{
    string encodedURL = "http://abc.com/psms/servlet/psms.Eservice2";

    HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(encodedURL);

    try
    {
        string xmlstr = "data=<!DOCTYPE REQUESTCREDIT SYSTEM \"http://127.0.0.1:80/ps/dtd/request.dtd\"><requestcredit username="\"abc\"" password="\"abc\""></requestcredit>&action=test";

        objRequest.Method = "POST";
        ASCIIEncoding objEncoding = new ASCIIEncoding();
        byte[] objBytes = objEncoding.GetBytes(xmlstr);
        objRequest.Timeout = 6000;
        objRequest.ContentType = "application/x-www-form-urlencoded";
        objRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
        objRequest.ContentLength = objBytes.Length;
        objRequest.KeepAlive = true;

        //objRequest.ServicePoint.ConnectionLimit = GlobalVariable.connectionLimit;
        Stream strm = objRequest.GetRequestStream();
        strm.Write(objBytes, 0, objBytes.Length);
        strm.Flush();
        strm.Close();

        //ServicePointManager.DefaultConnectionLimit = GlobalVariable.connectionLimit;
        using (HttpWebResponse WebResponse = (HttpWebResponse)objRequest.GetResponse())
        {
            Stream responseStream = default(Stream);
            responseStream = WebResponse.GetResponseStream();
            if ((WebResponse.ContentEncoding.ToLower().Contains("gzip")))
            {
                responseStream = new System.IO.Compression.GZipStream(responseStream, System.IO.Compression.CompressionMode.Decompress);
            }
            else if ((WebResponse.ContentEncoding.ToLower().Contains("deflate")))
            {
                responseStream = new System.IO.Compression.DeflateStream(responseStream, System.IO.Compression.CompressionMode.Decompress);
            }

            StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.Default);
            Response.Output.Write(reader.ReadToEnd());
            reader.Close();
            WebResponse.Close();
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {

    } objRequest.Abort();
}
Posted
v2
Comments
Sampath Lokuge 21-Feb-14 5:29am    
Check this : http://stackoverflow.com/questions/10604774/datastream-length-and-position-threw-an-exception-of-type-system-notsupportede
mayank.bhuvnesh 21-Feb-14 5:48am    
Sampath

It's still giving the same error.
I have even tried dis on different machines But no luck....
chetna2810 10-Mar-17 2:59am    
Hi,
I am facing the same error at my end. Kindly update if you have solution.

1 solution

I had the same problem.
I was googling and tying many of solutions but nothing worked.
At the end i just changes my URL links to "https://" in request and it worked!
 
Share this answer
 

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