HTTPWebResponse响应字符串被截断 [英] HTTPWebResponse Response string is truncated

查看:3319
本文介绍了HTTPWebResponse响应字符串被截断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

App正在与REST服务进行通信。
Fiddler在应用程序回复中显示完整的良好XML响应
应用程序在法属波利尼西亚和新西兰的相同副本工作,因此主要嫌疑人似乎编码但我们已经检查出来并空手而归。

App is talking to a REST service. Fiddler shows full good XML response coming in as the Apps response The App is in French Polynesia and an identical copy in NZ works so the prime suspect seemed encoding but we have checked that out and came up empty handed.

查看流阅读器中的输出字符串(UTF8编码),您可以看到它被截断的位置。它是一个无害的xml片段。 XmlDocument对象上的下游错误声称在将字符串加载到XML Document对象时遇到了意外的文件结尾,这对象是公平的。

Looking at the output string (UTF8 encoding) from the stream reader you can see where it has been truncated. It is in an innocuous piece of xml. The downstream error on an XmlDocument object claims to have an encountered an unexpected end of file while loading the string into an XML Document object which is fair enough.

截断点是

ns6:sts-krn> 1&

The truncation point is
ns6:sts-krn>1&

这是
ns6的一部分:sts-krn> 1< / ns6:sts -krn><

which is part of ns6:sts-krn>1</ns6:sts-krn><

响应字符串或我们应该检查的其他参数是否有任何大小限制。
我完全没有想法。代码按要求提供。

Is there any size limitation on the response string or some other parameter that we should check. I am all out of ideas. Code Supplied as requested.

Stream streamResponse = response.GetResponseStream();
StringBuilder sb = new StringBuilder();

Encoding encode = Encoding.GetEncoding("utf-8");
if (streamResponse != null)
{
    StreamReader readStream = new StreamReader(streamResponse, encode);
    while (readStream.Peek() >= 0)
    {
        sb.Append((char)readStream.Read());
    }
    streamResponse.Close();
}


推荐答案

你需要使用使用块:

using (WebResponse response = request.GetResponse())
{
    using (Stream streamResponse = response.GetResponseStream())
    {
        StringBuilder sb = new StringBuilder();

        if (streamResponse != null)
        {
            using (StreamReader readStream = new StreamReader(streamResponse, Encoding.UTF8))
            {
                sb.Append(readStream.ReadToEnd());
            }
        }
    }
 }

这个将确保您的 WebResponse StreamReader 全部无论是否有任何例外,都要清理干净。

This will ensure that your WebResponse, Stream, and StreamReader all get cleaned up, regardless of whether there are any exceptions.

导致我想到<$ c $的原因c>使用块是:


  1. 某些操作未完成

  2. 没有try / catch块隐藏异常,所以如果由于异常操作没有完成,我们就会知道它。

  3. 有些对象实现了 IDisposable 不在中使用

  1. Some operation was not completed
  2. There were no try/catch blocks hiding exceptions, so if the operation wasn't completed due to exceptions, we would know about it.
  3. There were objects which implement IDisposable which were not in using blocks

结论:尝试使用块实现,看看处置对象是否会导致操作完成。

Conclusion: try implementing the using blocks to see if disposing the objects will cause the operation to complete.

I这是因为推理实际上非常普遍。同样的推理适用于我的邮件消息不会被发送两分钟。在这种情况下,未完成的操作是发送电子邮件,实例是 SmtpClient MailMessage 对象。

I added this because the reasoning is actually quite general. The same reasoning works for "my mail message doesn't get sent for two minutes". In that case, the operation which isn't completed is "send email" and the instances are the SmtpClient and MailMessage objects.

这篇关于HTTPWebResponse响应字符串被截断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆