6

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

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

这是我们调用服务的方法:

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;
}

任何帮助将非常感激 ;-)

4

1 回答 1

9

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

它会合谋 .NET 框架包含这个恶魔属性,只要您的响应是文本/纯文本,它就可以工作,但如果它需要任何关闭 - 例如 XML 或 JSON - 它就会中断。

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

于 2013-05-08T13:16:29.853 回答