我在从响应中读取全部内容时遇到问题,解码器的长度不是按照响应内容的长度来的,我遇到了问题
int read = responseStream.EndRead(asyncResult);
//根据响应,读取的值非常小。
int len = rs.StreamDecode.GetChars(rs.BufferRead, 0, read, charBuffer, 0);
下面是我的相同代码
private void ReadQtoRCallBack(IAsyncResult asyncResult)
{
// Get the RequestState object from AsyncResult.
RequestState rs = (RequestState)asyncResult.AsyncState;
// Retrieve the ResponseStream that was set in RespCallback.
Stream responseStream = rs.ResponseStream;
// Read rs.BufferRead to verify that it contains data.
int read = responseStream.EndRead(asyncResult);
if (read > 0)
{
// Prepare a Char array buffer for converting to Unicode.
Char[] charBuffer = new Char[BUFFER_SIZE];
// Convert byte stream to Char array and then to String.
// len contains the number of characters converted to Unicode.
int len = rs.StreamDecode.GetChars(rs.BufferRead, 0, read, charBuffer, 0);
String str = new String(charBuffer, 0, len);
// Append the recently read data to the RequestData stringbuilder
// object contained in RequestState.
RequestData.Append(PureAnalyzer_WebApp.DoubleAt + rs.ResultName + PureAnalyzer_WebApp.DoubleColon);
// ESResultBE ESObj = new ESResultBE();
System.Web.Script.Serialization.JavaScriptSerializer JsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
ESResultBE ObjESResultBE;
string Result = "";
RequestData.Append(Encoding.ASCII.GetString(rs.BufferRead, 0, read).ToString());
// Continue reading data until
// responseStream.EndRead returns –1.
IAsyncResult ar = responseStream.BeginRead(
rs.BufferRead, 0, BUFFER_SIZE,
new AsyncCallback(ReadQtoRCallBack), rs);
}
else
{
// Close down the response stream.
responseStream.Close();
if (ResponseFlag[rs.ResultName] != "1")
{
ResponseFlag[rs.ResultName] = "1";
CompleteCount++;
}
// Set the ManualResetEvent so the main thread can exit.
allDone.Set();
}
return;
}