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""
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
Could there be also some character set mismatch? Look:
request.ContentType = "application/x-www-form-urlencoded; charset=
utf-8
";
byte[] bytes = Encoding.
ASCII
.GetBytes(postData);
And the error message just misleading...
Read the question carefully.
Understand that English isn't everyone's first language so be lenient of bad
spelling and grammar.
If a question is poorly phrased then either ask for clarification, ignore it, or
edit the question
and fix the problem. Insults are not welcome.
Don't tell someone to read the manual. Chances are they have and don't get it.
Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.