/// <summary> /// Attempt to enable output compression. /// </summary> /// <param name="target"></param> /// <param name="request"></param> /// <returns></returns> public static bool Compress(this HttpResponse target, HttpRequest request) { Contract.Requires(target != null, "target"); Contract.Requires(request != null, "request"); // Check encoding var acceptEncoding = request.Headers["Accept-Encoding"]; if (string.IsNullOrEmpty(acceptEncoding)) { return false; // Client did not announce any supported encodings - no compression } acceptEncoding = acceptEncoding.ToUpperInvariant(); // Try Deflate if (acceptEncoding.Contains("DEFLATE")) { target.Filter = new DeflateStream(target.Filter, CompressionMode.Compress); target.AppendHeader("Content-Encoding", "deflate"); return true; // Compress with deflate } // Try GZip if (acceptEncoding.Contains("GZIP")) { target.Filter = new GZipStream(target.Filter, CompressionMode.Compress); target.AppendHeader("Content-Encoding", "gzip"); return true; // Compress with gzip } return false; // No mutally agreeable compressions }
public static void AddCompressionHeaders(this HttpResponse response, CompressionSupport compressionSupport) { switch (compressionSupport) { case CompressionSupport.Deflate: response.AppendHeader(HttpExtensions.ResponseCompressionHeaderKey, HttpExtensions.DeflateIdentifier1); break; case CompressionSupport.GZip: response.AppendHeader(HttpExtensions.ResponseCompressionHeaderKey, HttpExtensions.GZipIdentifier1); break; default: break; } }
public static void ToExcel(this HttpResponseBase response, string filename) { response.AppendHeader("Content-Disposition", "attachment;filename=" + filename); response.Charset = "UTF-8"; response.ContentEncoding = System.Text.Encoding.Default; response.ContentType = "application/ms-excel"; }
/// <summary> /// Adds the specified encoding to the response header. /// </summary> /// <param name="response">An ASP.NET HTTP-response information</param> /// <param name="encoding">The ASP.NET page encoding</param> public static void SetHeaderEncodingType(this HttpResponse response, string encoding) { if (response != null) { response.AppendHeader("Content-encoding", encoding); } }
/// <summary> /// 发送405响应(不接受请求类型)到客户端 /// </summary> /// <param name="Response"></param> /// <param name="allow"></param> public static void Send405(this HttpResponseBase Response, HttpVerb allow) { Response.Clear(); Response.StatusCode = 405; Response.StatusDescription = "Method Not Allowed"; Response.AppendHeader("Allow", allow.ToString()); Response.Write(string.Format("Method Not Allowed, Except: {0}", allow.ToString())); Response.End(); }
/// <summary> /// 发送301响应(永久性重定向)到客户端 /// </summary> /// <param name="Response"></param> /// <param name="url">目标Url</param> public static void Send301(this HttpResponseBase Response, string url) { Response.Clear(); Response.StatusCode = 301; Response.StatusDescription = "Moved Permanently"; Response.AppendHeader("Location", url); Response.Write(string.Format("Object Moved Permanently, Location: {0}", url)); Response.End(); }
public static void NotModified(this HttpResponse response, string etag) { if (response == null) { throw new ArgumentNullException("response"); } response.Cache.SetETag(etag); response.AppendHeader("Content-Length", 0.ToString()); response.StatusCode = (int) HttpStatusCode.NotModified; }
/// <summary> /// 输出ExcelXml格式的Header /// </summary> /// <param name="response"></param> public static void AddExcelXmlHeader(this HttpResponse response, string fileNameWithoutExt) { ExceptionHelper.FalseThrow<ArgumentNullException>(response != null, "response"); string fileType = "text/xml"; string fileExt = "xml"; if (DeluxePrincipal.IsAuthenticated) { fileType = UserSettings.GetSettings(DeluxeIdentity.CurrentUser.ID).GetPropertyValue("CommonSettings", "downExcelXmlContentType", fileType); fileExt = UserSettings.GetSettings(DeluxeIdentity.CurrentUser.ID).GetPropertyValue("CommonSettings", "downExcelXmlFileExt", fileExt); } response.ContentType = fileType; response.AppendHeader("CONTENT-DISPOSITION", string.Format("{0};filename={1}", "inline", response.EncodeFileNameInContentDisposition(fileNameWithoutExt + "." + fileExt))); }
/// <summary> /// 写入Xml响应的头信息 /// </summary> /// <param name="response">Http响应</param> public static void AppendXmlHeader(this HttpResponse response) { response.AppendHeader("Content-Type", "text/xml; charset=UTF-8"); }
public static void ContentType(this IOutputWriter writer, MimeType mimeType) { writer.AppendHeader(HttpResponseHeader.ContentType, mimeType.Value); }
public static void AppendHeader(this IOutputWriter writer, HttpResponseHeader header, string value) { writer.AppendHeader(HttpResponseHeaders.HeaderNameFor(header), value); }
public static HttpListenerResponse Header(this HttpListenerResponse response, string name, string value) { response.AppendHeader(name, value); return response; }
public static void SetContentLength(this HttpResponse response, Int64 value) { response.AppendHeader("Content-Length", value.ToString(CultureInfo.InvariantCulture)); }
public static void CreatePDF(this HttpResponse response, byte[] pdf, string filename) { response.ClearContent(); response.ClearHeaders(); response.ContentType = "application/pdf"; //Response.ContentType = @"application/octet-stream"; response.AppendHeader("Content-Disposition", "attachment;filename=" + filename + ".pdf"); response.OutputStream.Write(pdf, 0, pdf.Length); response.BufferOutput = true; response.BinaryWrite(pdf); response.Flush(); response.End(); }