public HttpWebResponse Receive(ref byte[] payloadData)
{
if (this.logger != null)
{
this.logger.AddInfo("Start to receive HTTP response from server.");
}
int offset = 0;
try
{
this.httpWebResponse = (HttpWebResponse)this.httpWebRequest.GetResponse();
Stream responseStream = this.httpWebRequest.GetResponse().GetResponseStream();
if (this.httpWebResponse.Headers.GetValues("Content-Length") != null)
{
payloadData = new byte[this.httpWebResponse.ContentLength];
while (true)
{
offset += responseStream.Read(payloadData, offset, payloadData.Length - offset);
responseStream = this.httpWebRequest.GetResponse().GetResponseStream();
if (offset == payloadData.Length)
{
break;
}
}
}
else
{
List<byte> tempBuffer = new List<byte>();
byte[] payloadBuffer = new byte[this.bufferSize];
int readSize = 0;
while (true)
{
int tempIndex = 0;
readSize = responseStream.Read(payloadBuffer, 0, payloadBuffer.Length);
if (readSize == 0)
{
break;
}
else
{
tempBuffer.AddRange(GetBytes(payloadBuffer, ref tempIndex, readSize));
}
}
payloadData = tempBuffer.ToArray();
}
responseStream.Close();
responseStream.Dispose();
}
catch (WebException e)
{
if (this.logger != null)
{
this.logger.AddDebug(
string.Format("Unexpected exception is thrown, detailed information: {0}.", e.Message));
}
throw;
}
if (this.logger != null)
{
this.logger.AddInfo("The HTTP response is received successfully.");
}
return this.httpWebResponse;
}