Y
Young Cho
Hello!
I have a problem that is escaping me. I am trying to read the
HttpWebResponse by creating a StreamReader. On longer responses, couple of
hundred bytes at least, the code works fine. However, on shorter responses
(perhaps less than 100-200 characters), I keep getting a "The chunk length
was not valid exception". I've tried two different approaches and both work
with the longer response but exceptions out on the shorter responses. Here
are the two approaches:
//----- Approach 1 -----
// receiveStream = result.GetResponseStream();
// StreamReader sr = new StreamReader(receiveStream);
// resultstring = sr.ReadToEnd(); <--- exceptions here
//----- Approach 1 -----
char[] c = null;
receiveStream = result.GetResponseStream();
StreamReader sr = new StreamReader(receiveStream);
StringBuilder sb = new StringBuilder();
while (sr.Peek() >= 0) <----- exceptions here
{
c = new char[1024];
sr.Read(c, 0, c.Length);
sb.Append(c);
}
resultstring = sb.ToString();
Any suggestions or ideas? Thanks in advance! The full code is below if you
would like to see the context.
Young.
public void Send(object request, out object response)
{
HttpWebResponse result = null;
Stream requestStream = null;
Stream receiveStream = null;
string resultstring = "";
try
{
// Encode the authorization string to base 64
string authStr = SyncUtils.GetBase64EncodeStr("testuserassword");
// Create the HTTP POST request and the authentication headers
HttpWebRequest req =
(HttpWebRequest)WebRequest.Create("https://www.myserver.com/test");
req.Method = "POST";
req.ContentType = "text/xml";
req.Headers.Add("Cookie: SMCHALLENGE=YES");
req.Headers.Add("Authorization: Basic " + authStr);
// Add the POST payload data
byte[] encodedBytes = Encoding.UTF8.GetBytes(request.ToString());
req.AllowWriteStreamBuffering = true;
requestStream = req.GetRequestStream();
requestStream.Write(encodedBytes, 0 , encodedBytes.Length);
requestStream.Close();
result = (HttpWebResponse)req.GetResponse();
// Verify the returned HTTP code. If not OK (not 200), then
// we need to trigger some error handling & recovery.
if(result.StatusCode == HttpStatusCode.OK)
{
//----- Approach 1 -----
// receiveStream = result.GetResponseStream();
// StreamReader sr = new StreamReader(receiveStream);
// resultstring = sr.ReadToEnd();
//----- Approach 1 -----
char[] c = null;
receiveStream = result.GetResponseStream();
StreamReader sr = new StreamReader(receiveStream);
StringBuilder sb = new StringBuilder();
while (sr.Peek() >= 0)
{
c = new char[1024];
sr.Read(c, 0, c.Length);
sb.Append(c);
}
resultstring = sb.ToString();
}
else
{
// An HTTP code of non-200. Nothing to return.
resultstring = "";
}
}
catch(WebException we)
{
MessageBox(we.Message);
}
catch(Exception ex)
{
MessageBox(ex.Message);
}
finally
{
if( requestStream != null)
requestStream.Close();
if(receiveStream != null)
receiveStream.Close();
if( result != null )
result.Close();
}
response = resultstring;
}
I have a problem that is escaping me. I am trying to read the
HttpWebResponse by creating a StreamReader. On longer responses, couple of
hundred bytes at least, the code works fine. However, on shorter responses
(perhaps less than 100-200 characters), I keep getting a "The chunk length
was not valid exception". I've tried two different approaches and both work
with the longer response but exceptions out on the shorter responses. Here
are the two approaches:
//----- Approach 1 -----
// receiveStream = result.GetResponseStream();
// StreamReader sr = new StreamReader(receiveStream);
// resultstring = sr.ReadToEnd(); <--- exceptions here
//----- Approach 1 -----
char[] c = null;
receiveStream = result.GetResponseStream();
StreamReader sr = new StreamReader(receiveStream);
StringBuilder sb = new StringBuilder();
while (sr.Peek() >= 0) <----- exceptions here
{
c = new char[1024];
sr.Read(c, 0, c.Length);
sb.Append(c);
}
resultstring = sb.ToString();
Any suggestions or ideas? Thanks in advance! The full code is below if you
would like to see the context.
Young.
public void Send(object request, out object response)
{
HttpWebResponse result = null;
Stream requestStream = null;
Stream receiveStream = null;
string resultstring = "";
try
{
// Encode the authorization string to base 64
string authStr = SyncUtils.GetBase64EncodeStr("testuserassword");
// Create the HTTP POST request and the authentication headers
HttpWebRequest req =
(HttpWebRequest)WebRequest.Create("https://www.myserver.com/test");
req.Method = "POST";
req.ContentType = "text/xml";
req.Headers.Add("Cookie: SMCHALLENGE=YES");
req.Headers.Add("Authorization: Basic " + authStr);
// Add the POST payload data
byte[] encodedBytes = Encoding.UTF8.GetBytes(request.ToString());
req.AllowWriteStreamBuffering = true;
requestStream = req.GetRequestStream();
requestStream.Write(encodedBytes, 0 , encodedBytes.Length);
requestStream.Close();
result = (HttpWebResponse)req.GetResponse();
// Verify the returned HTTP code. If not OK (not 200), then
// we need to trigger some error handling & recovery.
if(result.StatusCode == HttpStatusCode.OK)
{
//----- Approach 1 -----
// receiveStream = result.GetResponseStream();
// StreamReader sr = new StreamReader(receiveStream);
// resultstring = sr.ReadToEnd();
//----- Approach 1 -----
char[] c = null;
receiveStream = result.GetResponseStream();
StreamReader sr = new StreamReader(receiveStream);
StringBuilder sb = new StringBuilder();
while (sr.Peek() >= 0)
{
c = new char[1024];
sr.Read(c, 0, c.Length);
sb.Append(c);
}
resultstring = sb.ToString();
}
else
{
// An HTTP code of non-200. Nothing to return.
resultstring = "";
}
}
catch(WebException we)
{
MessageBox(we.Message);
}
catch(Exception ex)
{
MessageBox(ex.Message);
}
finally
{
if( requestStream != null)
requestStream.Close();
if(receiveStream != null)
receiveStream.Close();
if( result != null )
result.Close();
}
response = resultstring;
}