如何缩小请求的大小? [英] How can I reduce size of a request?

查看:58
本文介绍了如何缩小请求的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个非常简单的数据输入页面,其中包含一些字段.当页面提交时,它发送一个大约2500字节的请求,我觉得这个请求非常高.理想情况下,它应该小于1500个字节,以便可以在一个数据包中传输数据.
我想知道如何最小化请求大小?我不在页面上存储cookie/会话.
我阅读了最小化请求开销 [

Hi,

I have a very simple data entry page with a few fields. When the page submits, it sends a request of about 2500 bytes which I feel is very high. Ideally it should be less than 1500 bytes so that the data can be transmitted in one packet.
I would like to know how can I minimize the request size? I am not storing cookies/sessions on the page.
I read Minimize request overhead[^] but there is not much i can implement from it.

Any help would be greatly appreciated.

[Update]
Thanks for the answer(Ilya Builuk).
Sorry that I forgot to mention I have already disabled ViewState for the page (the ViewState anyways was not much heavy for my page).

Regards
Ankur

推荐答案

您可以压缩ViewState数据:

1.在项目中添加
SharpZipLib 程序集.
2.将此代码添加到您的App_Code中:

You can compress your ViewState data:

1. Add SharpZipLib assembly in your project.
2. Add this code in your App_Code:

using System;
using System.IO;
using System.Web.UI;

using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
 
namespace PackViewState
 {
 	public class Page : System.Web.UI.Page
 	{
		public byte[] Compress(byte[] bytes)
 		{
 			MemoryStream memory = new MemoryStream();
 			Stream stream = new DeflaterOutputStream(memory,
 				new ICSharpCode.SharpZipLib.Zip.Compression.Deflater(
 				ICSharpCode.SharpZipLib.Zip.Compression.Deflater.BEST_COMPRESSION), 131072);
 			stream.Write(bytes, 0, bytes.Length);
 			stream.Close();
 			return memory.ToArray();
 		}
 
 		public byte[] Decompress(byte[] bytes)
 		{
 			Stream stream = new InflaterInputStream(new MemoryStream(bytes));
 			MemoryStream memory = new MemoryStream();
 			int totalLength = 0;
			byte[] writeData = new byte[4096];
 			while (true) 
 			{
 				int size = stream.Read(writeData, 0, writeData.Length);
				if (size > 0) 
 				{
 					totalLength += size;
 					memory.Write(writeData, 0, size);
				} 
 				else 
					break;
 			}
 			stream.Close();
 			return memory.ToArray();
 		}
 
 		protected override object LoadPageStateFromPersistenceMedium()
 		{
 			string vState = this.Request.Form["__VSTATE"];
			byte[] bytes = System.Convert.FromBase64String(vState);
 
 			bytes = this.Decompress(bytes);

			LosFormatter format = new LosFormatter();
			return format.Deserialize(System.Convert.ToBase64String(bytes));
		}

 		protected override void SavePageStateToPersistenceMedium(object viewState)
  		{
			LosFormatter format = new LosFormatter();
			StringWriter writer = new StringWriter();
			format.Serialize(writer, viewState);
			string viewStateStr = writer.ToString();

			byte[] bytes = System.Convert.FromBase64String(viewStateStr);
			bytes = this.Compress(bytes);
 
			string vStateStr = System.Convert.ToBase64String(bytes);

			RegisterHiddenField("__VSTATE", vStateStr);
		}
 	}
 }



3.从PackViewState.Page:
继承页面



3. Inherit your page from PackViewState.Page:

public class MyPage: PackViewState.Page
{
}


您好,Ankur回答了这个问题,因为问题状态尚未关闭(我知道您现在不需要此回答了:)).
您可以尝试 Yslow [加速网站的最佳做法 [如何在ASP.NET中减小页面大小 [
Hi Ankur, answering this one because the question status is not closed(I know you don''t need this answer now :) ).

You may try Yslow[^]

Best Practices for Speeding Up Your Web Site[^]

How To Reduce Page Size In ASP.NET[^]


这篇关于如何缩小请求的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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