GZipStream 压缩

uutwo 2009-03-03 09:44:30
项目当中需要用到 GZipStream 解压缩。我先把我写的方法帖出来大家帮忙看一下。

public static string Post(string Url, string RequestString, int TimeoutSeconds)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);

if (TimeoutSeconds > 0)
{
request.Timeout = 1000 * TimeoutSeconds;
}
request.Method = "POST";
request.AllowAutoRedirect = true;

byte[] data = System.Text.Encoding.GetEncoding("gb2312").GetBytes(RequestString);

if ((RequestString.Length > 4096) || (RequestString.IndexOf("transType=106") > -1))
{
try
{
request.ContentType = "application/x-gzip";
request.Accept = "application/x-gzip";
GZipStream outstream = new GZipStream(request.GetRequestStream(), CompressionMode.Compress);

outstream.Write(data, 0, data.Length);
outstream.Close();
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
}
else
{
request.ContentType = "application/x-www-form-urlencoded";
request.Accept = "";
Stream outstream = request.GetRequestStream();

outstream.Write(data, 0, data.Length);
outstream.Close();
}

HttpWebResponse hwrp = (HttpWebResponse)request.GetResponse();
string ContentType = hwrp.Headers.Get("Content-Type");

StreamReader sr = null;

if (ContentType.IndexOf("text/html") > -1)
{
sr = new StreamReader(hwrp.GetResponseStream(), System.Text.Encoding.GetEncoding("gb2312"));
}
else
{
sr = new StreamReader(new GZipStream(hwrp.GetResponseStream(), CompressionMode.Decompress), System.Text.Encoding.GetEncoding("gb2312"));
}

return sr.ReadToEnd();
}

这个方法会出现一个非常奇怪的问题,当我和别人进行联调的时候发现这个方法前面两次压缩是正常的,当进行到第三次压缩的时候就会出现非常异常的错误,当程序进行第三次压缩的时候程序不再运行下去了,用 try 也不行。
...全文
159 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
uutwo 2009-04-01
  • 打赏
  • 举报
回复
这个帖子就这沉了吗。。。。高手请进啊。
uutwo 2009-03-06
  • 打赏
  • 举报
回复
这个帖子就这沉了吗。。。。高手请进啊。
uutwo 2009-03-04
  • 打赏
  • 举报
回复
是因为资源没有回收的原因吗?最开始我也是这样怀疑的,但我把这处程序在执行的时候进行休眠 1 分钟,并且在程序的后面把需要回收的都进行了回收,运行程序出现的状态还是一样的。
悔说话的哑巴 2009-03-04
  • 打赏
  • 举报
回复
这个不会 帮顶!!!!
sxmonsy 2009-03-03
  • 打赏
  • 举报
回复
帮顶,和LZ一起等高人来
  • 打赏
  • 举报
回复
具体阻塞在哪里了呢?

你的程序好像最后没有关闭request吧。
happychou 2009-03-03
  • 打赏
  • 举报
回复
关注学习。。。
lianhui1122 2009-03-03
  • 打赏
  • 举报
回复
dd
uutwo 2009-03-03
  • 打赏
  • 举报
回复
GZipStream outstream = new GZipStream(request.GetRequestStream(), CompressionMode.Compress);

outstream.Write(data, 0, data.Length);
outstream.Flush();
outstream.Close();
GC.WaitForPendingFinalizers();


加了 GC.WaitForPendingFinalizers() 还是不行,还是出现同样的状态。
uutwo 2009-03-03
  • 打赏
  • 举报
回复
request.GetRequestStream() 返回的也是一个压缩流。
cancerser 2009-03-03
  • 打赏
  • 举报
回复
request.GetRequestStream() 这东西返回是一个流吧? 是不是应该关闭下?还有下面那个StreamReader
在说有流关闭前调用Flush()方法

GZipStream 是否会调用到非托管资源,如果访问频率较高,这地方需要互斥下
  • 打赏
  • 举报
回复
关闭后request调用
GC.WaitForPendingFinalizers();
就是保证当前的Stream一定关闭并释放了句柄,否则句柄在没释放之前,如果你再次请求就处于锁死状态了。

不但是request,所有stream都主动调用close一下。
uutwo 2009-03-03
  • 打赏
  • 举报
回复
TGZipCompressBar 不行的。 因为这是和另一家公司进行数据通讯。
mengfeihui 2009-03-03
  • 打赏
  • 举报
回复
学习
uutwo 2009-03-03
  • 打赏
  • 举报
回复
关闭 request 也还是没用。
uutwo 2009-03-03
  • 打赏
  • 举报
回复
关闭 request 也还是这样。
uutwo 2009-03-03
  • 打赏
  • 举报
回复
GZipStream outstream = new GZipStream(request.GetRequestStream(), CompressionMode.Compress);
就是这一句,当第三次运行到这一句的时候程序就不再运行下去。用 try 也没有错误出来。

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧