uniblab

handy cheat for downloading string content from web requests

Jan 3rd, 2020
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.70 KB | None | 0 0
  1. using System.Linq;
  2.  
  3. namespace Icod.Wod {
  4.  
  5.     [System.Xml.Serialization.XmlType( IncludeInSchema = false )]
  6.     public static class StringHelper {
  7.  
  8.         public static System.String TrimToNull( this System.String @string ) {
  9.             if ( System.String.IsNullOrEmpty( @string ) ) {
  10.                 return null;
  11.             }
  12.             @string = @string.Trim();
  13.             if ( System.String.IsNullOrEmpty( @string ) ) {
  14.                 return null;
  15.             }
  16.             return @string;
  17.         }
  18.  
  19.         public static System.Byte[] Compress(
  20.             this System.String @string,
  21.             System.Text.Encoding encoding,
  22.             System.Func<System.IO.Stream, System.IO.Compression.CompressionMode, System.Boolean, System.IO.Stream> compressor
  23.         ) {
  24.             using ( var input = new System.IO.MemoryStream( encoding.GetBytes( @string ), false ) ) {
  25.                 using ( var worker = compressor( input, System.IO.Compression.CompressionMode.Compress, true ) ) {
  26.                     using ( var output = new System.IO.MemoryStream() ) {
  27.                         worker.CopyTo( output );
  28.                         output.Flush();
  29.                         output.Seek( 0, System.IO.SeekOrigin.Begin );
  30.                         return output.ToArray();
  31.                     }
  32.                 }
  33.             }
  34.         }
  35.         public static System.Byte[] Gzip( this System.String @string, System.Text.Encoding encoding ) {
  36.             return @string.Compress(
  37.                 encoding,
  38.                 ( stream, compressionMode, leaveOpen ) => new System.IO.Compression.GZipStream( stream, compressionMode, leaveOpen )
  39.             );
  40.         }
  41.         public static System.Byte[] Deflate( this System.String @string, System.Text.Encoding encoding ) {
  42.             return @string.Compress(
  43.                 encoding,
  44.                 ( stream, compressionMode, leaveOpen ) => new System.IO.Compression.DeflateStream( stream, compressionMode, leaveOpen )
  45.             );
  46.         }
  47.  
  48.         public static System.String GetString( this System.Byte[] response, System.Text.Encoding encoding ) {
  49.             return encoding.GetString( response );
  50.         }
  51.         public static System.String Decompress(
  52.             this System.Byte[] response,
  53.             System.Text.Encoding encoding,
  54.             System.Func<System.IO.Stream, System.IO.Compression.CompressionMode, System.Boolean, System.IO.Stream> decompressor
  55.         ) {
  56.             using ( var input = new System.IO.MemoryStream( response, false ) ) {
  57.                 using ( var worker = decompressor( input, System.IO.Compression.CompressionMode.Decompress, true ) ) {
  58.                     using ( var output = new System.IO.MemoryStream() ) {
  59.                         worker.CopyTo( output );
  60.                         output.Flush();
  61.                         output.Seek( 0, System.IO.SeekOrigin.Begin );
  62.                         return output.ToArray().GetString( encoding );
  63.                     }
  64.                 }
  65.             }
  66.         }
  67.         public static System.String Gunzip( this System.Byte[] response, System.Text.Encoding encoding ) {
  68.             return response.Decompress(
  69.                 encoding,
  70.                 ( stream, compressionMode, leaveOpen ) => new System.IO.Compression.GZipStream( stream, compressionMode, leaveOpen )
  71.             );
  72.         }
  73.         public static System.String Inflate( this System.Byte[] response, System.Text.Encoding encoding ) {
  74.             return response.Decompress(
  75.                 encoding,
  76.                 ( stream, compressionMode, leaveOpen ) => new System.IO.Compression.DeflateStream( stream, compressionMode, leaveOpen )
  77.             );
  78.         }
  79.  
  80.         public static System.String GetWebString( this System.Byte[] response, System.Text.Encoding encoding, System.String contentEncoding ) {
  81.             return ( contentEncoding.TrimToNull() ?? "identity" ).Equals( "identity", System.StringComparison.OrdinalIgnoreCase )
  82.                 ? response.GetString( encoding )
  83.                 : contentEncoding.Equals( "gzip", System.StringComparison.OrdinalIgnoreCase )
  84.                     ? response.Gunzip( encoding )
  85.                     : contentEncoding.Equals( "deflate", System.StringComparison.OrdinalIgnoreCase )
  86.                         ? response.Inflate( encoding )
  87.                         : throw new System.InvalidOperationException( System.String.Format(
  88.                             "Unknown Content-Encoding value received from server: {0}",
  89.                             contentEncoding
  90.                         ) )
  91.             ;
  92.         }
  93.  
  94.     }
  95.  
  96. }
Add Comment
Please, Sign In to add comment