Click here to Skip to main content
15,910,603 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Here is my Code

C#
private static string PostResponse(string url, string postData, string userAgent = null)
        {
            string str = string.Empty;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            ASCIIEncoding encoding = new ASCIIEncoding();
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded; charset=utf-8";
            if (userAgent != null)
            {
                request.UserAgent = userAgent;
            }
            using (Stream stream = request.GetRequestStream())
            {
                byte[] bytes = Encoding.ASCII.GetBytes(postData);
                stream.Write(bytes, 0, bytes.Length);
                stream.Close();
            }
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (Stream stream2 = response.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(stream2))
                    {
                        str = reader.ReadToEnd();
                        reader.Close();
                    }
                    stream2.Close();
                }
                response.Close();
            }
            return str;
        }



After posting getting error like Length = 's.Length' threw an exception of type 'System.NotSupportedException""
Posted
Updated 7-Aug-13 1:14am
v2

Not all streams support that method. A FileStream, for example, does as it's possible to know exactly how large the file is when the stream to it is opened.

A the GetResponseStream can be one of many implementations (ConnectStream, GZipWrapperStream, DeflateWrapperStream, etc.) and not all of these will know up front the length of the payload. Because of this, Stream.Length throws NotSupportedException.

Instead of trying to read to end, try reading chunks of data until there no more available or the stream has been closed.

Hope this helps,
Fredrik
 
Share this answer
 
Comments
Mukesh Ghosh 7-Aug-13 9:38am    
Can you give me some code block or reference?
Fredrik Bornander 7-Aug-13 9:58am    
Something like this might work;

using(StreamReader reader = null)
{
var builder = new StringBuilder();
var buffer = new char[1024];

int read;
while ((read = reader.Read(buffer, 0, buffer.Length)) != -1)
{
builder.Append(buffer, 0, read);
}

str = builder.ToString();
}
Mukesh Ghosh 9-Aug-13 1:38am    
Not ,working.
Could there be also some character set mismatch? Look:
request.ContentType = "application/x-www-form-urlencoded; charset=utf-8";
and
byte[] bytes = Encoding.ASCII.GetBytes(postData);
And the error message just misleading...
 
Share this answer
 
Comments
Mukesh Ghosh 9-Aug-13 1:38am    
So ,what is the solution?

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