HttpWebRequest Response Stream仅返回64k的数据

时间:2013-05-03 09:35:32

标签: c# .net rest httpwebrequest client

我们正在使用Web API为我们的应用程序提供RESTful服务,该服务目前正在使用WPF,因此需要服务客户端。

我们遇到的问题是,当响应大小超过64k时,基础流停止在64k并且如果请求更多数据则抛出异常,尽管ContentLength更大(在测试用例中~125k)< / p>

以下是我们调用服务的方法:

private HttpWebResponse CallServiceMethod<TDto>(Uri serviceURL, string method, out WebException requestExceptionDetail, TDto dto = null) where TDto : DTOBase
{
  HttpWebRequest request;
  HttpWebResponse response;
  Encoding enc;
  string encodedDto;
  byte[] encodedDtoArray;
  Stream writeStream;

  request = WebRequest.CreateHttp(serviceURL);
  request.Method = method;

  if (dto != null)
  {
    enc = UTF8Encoding.Default;
    encodedDto = String.Concat("=", JsonConvert.SerializeObject(dto));
    encodedDtoArray = enc.GetBytes(encodedDto);

    request.ContentLength = encodedDtoArray.Length;
    request.ContentType = "application/x-www-form-urlencoded";

    writeStream = request.GetRequestStream();
    writeStream.Write(encodedDtoArray, 0, encodedDtoArray.Length);
    writeStream.Close();
  }

  requestExceptionDetail = null;

  try
  {
    response = (HttpWebResponse)request.GetResponse();
  }
  catch (WebException wex)
  {
    response = (HttpWebResponse)wex.Response;
    requestExceptionDetail = wex;
  }

  return response;
}

...这里是一个解码响应的方法,其间有一些胶合逻辑传递响应。

private TObjectType DecodeResponse<TObjectType>(HttpWebResponse webResponse)
{
  Encoding enc;
  StreamReader responseReader;
  string responseText;
  TObjectType retVal;

  // Obtain a string from the underlying response string
  enc = UTF8Encoding.Default;
  responseReader = new StreamReader(webResponse.GetResponseStream(), enc);
  responseText = responseReader.ReadToEnd();

  // Use JSon to deserialise the string
  retVal = JsonConvert.DeserializeObject<TObjectType>(responseText);

  return retVal;
}

我们也试过这种事情,但无济于事:

private TObjectType DecodeResponse<TObjectType>(HttpWebResponse webResponse)
{
  Encoding enc;
  StreamReader responseReader;
  string responseText;
  TObjectType retVal;
  char[] buffer;
  int bufferSize, startPos, totalLength, readLength;
  StringBuilder sbx;

  // Obtain a string from the underlying response string
  enc = UTF8Encoding.Default;
  responseReader = new StreamReader(webResponse.GetResponseStream(), enc);

  totalLength = (int)webResponse.ContentLength;
  bufferSize = 65536;
  startPos = 0;
  buffer = new char[bufferSize];

  sbx = new StringBuilder(totalLength);
  //webResponse.GetResponseStream().

  while (startPos < totalLength)
  {
    readLength = Math.Min(bufferSize, totalLength - startPos);
    responseReader.Read(buffer, startPos, readLength);
    sbx.Append(buffer, 0, readLength);

    startPos += readLength;
  }

  //responseText = responseReader.ReadToEnd();
  responseText = sbx.ToString();

  // Use JSon to deserialise the string
  retVal = JsonConvert.DeserializeObject<TObjectType>(responseText);

  return retVal;
}

非常感谢任何帮助; - )

1 个答案:

答案 0 :(得分:7)

进一步调查显示HttpWebRequest有一个名为DefaultMaximumErrorResponseLength的静态属性,默认设置为65536。

它会合谋.NET框架包含这个恶魔属性,只要你的响应是text / plain,但是如果它需要任何闭包 - 例如XML或JSON - 就会破坏它。

对于需要更多信息的任何人,请参阅: