Example #1
0
        // GET: Api/Post/Add3
        public JsonResult Add3()
        {
            PostModel model = new PostModel();
            model.PDate = DateTime.Now;

            string fileName = Server.MapPath("~/App_Data/test3.json");
            model.PText = fileName;

            System.Runtime.Serialization.Json.DataContractJsonSerializer ser =
                new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(PostModel));

            System.IO.MemoryStream stream1 = new System.IO.MemoryStream();

            ser.WriteObject(stream1, model);

            using (System.IO.FileStream f2 = new System.IO.FileStream(fileName, System.IO.FileMode.Create))
            {
                byte[] jsonArray = stream1.ToArray();

                using (System.IO.Compression.GZipStream gz =
                    new System.IO.Compression.GZipStream(f2, System.IO.Compression.CompressionMode.Compress))
                {
                    gz.Write(jsonArray, 0, jsonArray.Length);
                }
            }

            return Json(model, JsonRequestBehavior.AllowGet);
        }
Example #2
0
        /// <summary>
        /// In Memory GZip Decompressor 
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] GZip_Decompress(this byte[] data)
        {
            int length = 100000; //10Kb
            byte[] Ob = new byte[length];
            byte[] result = null;

            using (var ms = new MemoryStream(data))
            {
                using (var gz = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress))
                {
                    int a = 0;
                    while ((a = gz.Read(Ob, 0, length)) > 0)
                    {
                        if (a == length)
                            result = result.Concat(Ob);
                        else
                            result = result.Concat(Ob.Substring(0, a));
                    }
                    gz.Close();
                }
                ms.Close();
            }

            return result;
        }
Example #3
0
 public static byte[] Decompress(byte[] data)
 {
     try
     {
         MemoryStream ms = new MemoryStream(data);
         System.IO.Compression.GZipStream zip = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress, true);
         MemoryStream msreader = new MemoryStream();
         byte[] buffer = new byte[0x1000];
         while (true)
         {
             int reader = zip.Read(buffer, 0, buffer.Length);
             if (reader <= 0)
             {
                 break;
             }
             msreader.Write(buffer, 0, reader);
         }
         zip.Close();
         ms.Close();
         msreader.Position = 0;
         buffer = msreader.ToArray();
         msreader.Close();
         return buffer;
     }
     catch (Exception e)
     {
         throw new Exception(e.Message);
     }
 }
Example #4
0
        /// <summary>
        /// �����ѹ���������л�
        /// </summary>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public static object Decompress(byte[] buffer, Type type)
        {
            System.IO.MemoryStream ms3 = new System.IO.MemoryStream();
            System.IO.MemoryStream ms2 = new System.IO.MemoryStream(buffer);
            System.IO.Compression.GZipStream gs = new System.IO.Compression.GZipStream(ms2, System.IO.Compression.CompressionMode.Decompress);

            byte[] writeData = new byte[4096];

            while (true)
            {
                int size = gs.Read(writeData, 0, writeData.Length);
                if (size > 0)
                {
                    ms3.Write(writeData, 0, size);
                }
                else
                {
                    break;
                }
            }

            gs.Close();
            ms3.Flush();
            byte[] DecompressBuf = ms3.ToArray();

            #region deserialize
            CompressionSerialize compressionSerialize = new CompressionSerialize();
            return compressionSerialize.Deserialize(DecompressBuf);

            #endregion
        }
        // use the cookiecontainter to retrieve the content of the given url
        public static string GetWebPageContent(string url, CookieContainer cookieContainer = null)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.MaximumAutomaticRedirections = 4;
            request.MaximumResponseHeadersLength = 4;
            request.Method = "GET";
            request.Credentials = CredentialCache.DefaultCredentials;
            request.CookieContainer = cookieContainer;
            request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            Stream responseStream = response.GetResponseStream();
            if (response.ContentEncoding.ToLower().Contains("gzip"))
                responseStream = new System.IO.Compression.GZipStream(responseStream, System.IO.Compression.CompressionMode.Decompress);
            else if (response.ContentEncoding.ToLower().Contains("deflate"))
                responseStream = new System.IO.Compression.DeflateStream(responseStream, System.IO.Compression.CompressionMode.Decompress);

            StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8);

            string html = readStream.ReadToEnd();

            response.Close();
            responseStream.Close();
            return html;
        }
        public static string UnZip(string value)
        {
            //Transform string into byte[]
            byte[] byteArray = new byte[value.Length];
            int indexBA = 0;
            foreach (char item in value.ToCharArray())
            {
                byteArray[indexBA++] = (byte)item;
            }

            //Prepare for decompress
            System.IO.MemoryStream ms = new System.IO.MemoryStream(byteArray);
            System.IO.Compression.GZipStream sr = new System.IO.Compression.GZipStream(ms,
                System.IO.Compression.CompressionMode.Decompress);

            //Reset variable to collect uncompressed result
            byteArray = new byte[byteArray.Length];

            //Decompress
            int rByte = sr.Read(byteArray, 0, byteArray.Length);

            //Transform byte[] unzip data to string
            System.Text.StringBuilder sB = new System.Text.StringBuilder(rByte);
            //Read the number of bytes GZipStream red and do not a for each bytes in
            //resultByteArray;
            for (int i = 0; i < rByte; i++)
            {
                sB.Append((char)byteArray[i]);
            }
            sr.Close();
            ms.Close();
            sr.Dispose();
            ms.Dispose();
            return sB.ToString();
        }
Example #7
0
        private static string GetResponseAsString(HttpWebResponse rsp, Encoding encoding)
        {
            Stream stream = null;
            StreamReader reader = null;

            try {
                // 以字符流的方式读取HTTP响应
                stream = rsp.GetResponseStream();
                switch (rsp.ContentEncoding) {
                    case "gzip":
                        stream = new System.IO.Compression.GZipStream(stream, System.IO.Compression.CompressionMode.Decompress);
                        break;

                    case "deflate":
                        stream = new System.IO.Compression.DeflateStream(stream, System.IO.Compression.CompressionMode.Decompress);
                        break;
                }
                reader = new StreamReader(stream, encoding);

                return reader.ReadToEnd();
            }
            finally {
                // 释放资源
                if (reader != null) reader.Close();
                if (stream != null) stream.Close();
                if (rsp != null) rsp.Close();
            }
        }
        /// <summary>
        /// Компрессия или декомпрессия файла
        /// </summary>
        /// <param name="fromFile">Исходный файл для компрессии или декомпрессии</param>
        /// <param name="toFile">Целевой файл</param>
        /// <param name="compressionMode">Указывает на компрессию или декомпрессию</param>
        private static void CompressOrDecompressFile(string fromFile, string toFile, System.IO.Compression.CompressionMode compressionMode)
        {
            System.IO.FileStream toFs = null;
            System.IO.Compression.GZipStream gzStream = null;
            System.IO.FileStream fromFs = new System.IO.FileStream(fromFile, System.IO.FileMode.Open, System.IO.FileAccess.Read);

            try
            {
                toFs = new System.IO.FileStream(toFile, System.IO.FileMode.Create, System.IO.FileAccess.Write);
                gzStream = new System.IO.Compression.GZipStream(toFs, compressionMode);
                byte[] buf = new byte[fromFs.Length];
                fromFs.Read(buf, 0, buf.Length);
                gzStream.Write(buf, 0, buf.Length);
            }
            finally
            {
                if (gzStream != null)
                    gzStream.Close();

                if (toFs != null)
                    toFs.Close();

                fromFs.Close();
            }
        }
Example #9
0
        public static string ZipCompress(this string value)
        {
            //Transform string into byte[]  
            byte[] byteArray = new byte[value.Length];
            int indexBA = 0;
            foreach (char item in value.ToCharArray())
            {
                byteArray[indexBA++] = (byte)item;
            }

            //Prepare for compress
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            System.IO.Compression.GZipStream sw = new System.IO.Compression.GZipStream(ms,
                System.IO.Compression.CompressionMode.Compress);

            //Compress
            sw.Write(byteArray, 0, byteArray.Length);
            //Close, DO NOT FLUSH cause bytes will go missing...
            sw.Close();

            //Transform byte[] zip data to string
            byteArray = ms.ToArray();
            System.Text.StringBuilder sB = new System.Text.StringBuilder(byteArray.Length);
            foreach (byte item in byteArray)
            {
                sB.Append((char)item);
            }
            ms.Close();
            sw.Dispose();
            ms.Dispose();
            return sB.ToString();
        }
Example #10
0
        private void SaveWebPost(string fileName, PostModel model)
        {
            WebPostModel wPost = new WebPostModel()
            {
                PTitle = model.PTitle,
                PText = model.PText,
                PLink = model.PLink,
                PImage = model.PImage,
                PDate = model.PDate.ToString("yyyy-MM-ddTHH:mm:ss"),
                PPrice = model.PPrice
            };

            System.IO.MemoryStream msPost = new System.IO.MemoryStream();
            System.Runtime.Serialization.Json.DataContractJsonSerializer dcJsonPost =
                new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(WebPostModel));
            dcJsonPost.WriteObject(msPost, wPost);

            using (System.IO.FileStream f2 = new System.IO.FileStream(fileName, System.IO.FileMode.Create))
            {
                byte[] jsonArray = msPost.ToArray();

                using (System.IO.Compression.GZipStream gz =
                    new System.IO.Compression.GZipStream(f2, System.IO.Compression.CompressionMode.Compress))
                {
                    gz.Write(jsonArray, 0, jsonArray.Length);
                }
            }
        }
 public override object Deserialize(Type type, Stream stream)
 {
     using (var gzip = new System.IO.Compression.GZipStream(stream, System.IO.Compression.CompressionMode.Decompress))
     using (var sr = new StreamReader(gzip, this.Encoding))
     {
         return new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize(sr.ReadToEnd(), type);
     }
 }
Example #12
0
        /// <summary>
        /// Decompress with Stream
        /// </summary>
        public static void Decompress(Stream input)
        {
            System.IO.Compression.GZipStream stream =
                new System.IO.Compression.GZipStream(
                   input, System.IO.Compression.CompressionMode.Decompress);

            stream.Flush();
        }
Example #13
0
 /// <summary>
 /// This function return a byte array compressed by GZIP algorithm.
 /// </summary>
 /// <param name="data"></param>
 /// <returns></returns>
 public static byte[] CompressGZIP(byte[] data)
 {
     System.IO.MemoryStream streamoutput = new System.IO.MemoryStream();
     System.IO.Compression.GZipStream gzip = new System.IO.Compression.GZipStream(streamoutput, System.IO.Compression.CompressionMode.Compress, false);
     gzip.Write(data, 0, data.Length);
     gzip.Close();
     return streamoutput.ToArray();
 }
 /// <summary>
 /// Decode the content
 /// </summary>
 /// <param name="data">Content to decode</param>
 /// <returns>Decoded content</returns>
 public byte[] Decode(byte[] data)
 {
     var output = new MemoryStream();
     var input = new MemoryStream(data);
     using (var stream = new System.IO.Compression.GZipStream(input, System.IO.Compression.CompressionMode.Decompress))
         stream.CopyTo(output);
     return output.ToArray();
 }
 public override void Serialize(Stream stream, object obj)
 {
     var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(obj);
     var data = this.Encoding.GetBytes(json);
     using (var gzip = new System.IO.Compression.GZipStream(stream, System.IO.Compression.CompressionLevel.Fastest))
     {
         gzip.Write(data, 0, data.Length);
     }
 }
		/// <summary>
		/// Gets the response stream with HTTP decompression.
		/// </summary>
		/// <param name="response">The response.</param>
		/// <returns></returns>
		public static Stream GetResponseStreamWithHttpDecompression(this WebResponse response)
		{
			var stream = response.GetResponseStream();
			var encoding = response.Headers["Content-Encoding"];
			if (encoding != null && encoding.Contains("gzip"))
				stream = new System.IO.Compression.GZipStream(stream, System.IO.Compression.CompressionMode.Decompress);
			else if (encoding != null && encoding.Contains("deflate"))
				stream = new System.IO.Compression.DeflateStream(stream, System.IO.Compression.CompressionMode.Decompress);
			return stream;
		}
 protected override object DeserializeCore(Type type, byte[] value)
 {
     using (var ms = new MemoryStream(value))
     using (var gzip = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress))
     using (var sr = new StreamReader(gzip, Encoding.UTF8))
     {
         var result = new JsonSerializer().Deserialize(sr, type);
         return result;
     }
 }
Example #18
0
		/// <summary>
		/// Gets the response stream with HTTP decompression.
		/// </summary>
		/// <param name="response">The response.</param>
		/// <returns></returns>
		public static Stream GetResponseStreamWithHttpDecompression(this HttpResponseMessage response)
		{
			var stream = response.Content.ReadAsStreamAsync().Result;
			var encoding = response.Headers.GetValues("Content-Encoding");
			if (encoding != null && encoding.Contains("gzip"))
				stream = new System.IO.Compression.GZipStream(stream, System.IO.Compression.CompressionMode.Decompress);
			else if (encoding != null && encoding.Contains("deflate"))
				stream = new System.IO.Compression.DeflateStream(stream, System.IO.Compression.CompressionMode.Decompress);
			return stream;
		}
        internal dynamic GetObjectResponse(IInternetServiceListener Listener = null)
        {
            IHttpWebResponse response = GetResponse(Listener);
            if (response == null)
                return null;

            HttpStatusCode sc = response.StatusCode;

            if (sc != HttpStatusCode.OK)
            {
                if (Listener != null)
                    Listener.OnStatusCodeKO(sc);

                return null;
            }

            Stream readStream = response.GetResponseStream();
            if (readStream == null)
            {
                if (Listener != null)
                    Listener.OnUnExpectedUnreadableResult();

                return null;
            }

            Stream toberead = null;

            if (response.ContentEncoding == "gzip")
            {
                toberead = new MemoryStream();

                using (Stream unzip = new System.IO.Compression.GZipStream(readStream, System.IO.Compression.CompressionMode.Decompress))
                {
                    unzip.CopyTo(toberead); 
                }

                toberead.Position = 0;
                readStream.Dispose();
            }
            else
                toberead = readStream;


            string sr = null;
            using (toberead)
            {
                using (StreamReader reader = new StreamReader(toberead))
                {
                    sr = reader.ReadToEnd();
                }

            }

            return DynamicJsonConverter.DynamicDeSerialize(sr);
        }
        public override PasswordQuality Evaluate(PwEntry entry)
        {
            string usrname = GetValue(entry, EntryDataType.UserName);
            string pwd = GetValue(entry, EntryDataType.Password);
            BuiltInStrengthMeasure bism = new BuiltInStrengthMeasure();
            BasicStrengthMeasure bsm = new BasicStrengthMeasure();
            int strengthA = (int)bism.Evaluate(entry);
            int strengthB = (int)bsm.Evaluate(entry);
            int strength = (int)Math.Round((decimal)(strengthA + strengthB) / 2);
            bool containsDictWord = false;
            bool containsPartOfUsrName = false;
            bool containsReverseUsrName = false;
            bool containsReversePartOfUsrName = false;
            string reverseUsername = Reverse(usrname);
            try {

                /*
                 * I have put into resources with GZip compression, instead of file.(Peter Torok)
                 * If you place it uncompressed use this:
                 * System.IO.StringReader dict = new System.IO.StringReader(Resources.dictionary);
                 */

                System.IO.Stream resource_stream = new System.IO.MemoryStream(Resources.dictionary_gzip);
                System.IO.Compression.GZipStream gz_stream = new System.IO.Compression.GZipStream(resource_stream, System.IO.Compression.CompressionMode.Decompress);
                System.IO.StreamReader dict = new System.IO.StreamReader(gz_stream);

                //System.IO.FileStream fileStream = System.IO.File.OpenRead("dictionary.txt");
                //System.IO.StreamReader dict = new System.IO.StreamReader(fileStream);
                //System.IO.StringReader dict = new System.IO.StringReader(streamReader.ReadToEnd());

                //TEST
                //System.Windows.Forms.MessageBox.Show(dict.ReadLine());

                string dictWord = null;
                while ((dictWord = dict.ReadLine()) != null)
                    if (pwd.Contains(dictWord)) containsDictWord = true;
            } catch (Exception e) {
                System.Windows.Forms.MessageBox.Show("An error has occured!\nDetails: " + e.ToString());
                return PasswordQuality.Error;
            }
            for (int i = 3; i < usrname.Length; i++)
                for (int j = 0; j < usrname.Length - i; j++)
                    if (pwd.Contains(usrname.Substring(j, i))) containsPartOfUsrName = true;
            for (int i = 3; i <= reverseUsername.Length; i++)
                for (int j = 0; j < reverseUsername.Length - i; j++)
                    if (pwd.Contains(reverseUsername.Substring(j, i))) containsReverseUsrName = true;
            for (int i = 3; i < usrname.Length; i++)
                for (int j = 0; j < usrname.Length - i; j++)
                    if (pwd.Contains(Reverse(usrname.Substring(j, i)))) containsReversePartOfUsrName = true;
            if (pwd.Contains(usrname) || containsPartOfUsrName || containsReverseUsrName || containsReversePartOfUsrName) strength--;
            if (containsDictWord) strength--;
            if (strength < 0) strength = 0;
            return (PasswordQuality)strength;
        }
Example #21
0
 public override byte[] Decode(byte[] Input)
 {
     switch (Input [0]) {
     case 0:
         System.IO.Compression.GZipStream gzs = new System.IO.Compression.GZipStream (new System.IO.MemoryStream (Input, 1, Input.Length - 1), System.IO.Compression.CompressionMode.Decompress);
         System.IO.MemoryStream rms/*i didn't mean it, it happend*/ = new System.IO.MemoryStream (Input.Length * 3);
         gzs.CopyTo (rms);
         return rms.ToArray ();
     default:
         throw new NotSupportedException ("Invalid/Unsupported compression algorithm.");
     }
 }
Example #22
0
        /// <summary>
        /// Decompress data using GZip
        /// </summary>
        /// <param name="dataToDecompress">The stream that hold the data</param>
        /// <returns>Bytes array of decompressed data</returns>
        public static byte[] Decompress(Stream dataToDecompress)
        {
            MemoryStream target = new MemoryStream();

            using (System.IO.Compression.GZipStream decompressionStream = new System.IO.Compression.GZipStream(dataToDecompress,
                System.IO.Compression.CompressionMode.Decompress))
            {
                decompressionStream.CopyTo(target);
            }
            return target.GetBuffer();

        }
 public void Run()
 {
     try
       {
     while (_running)
     {
       string xml = @"<?xml version=""1.0"" ?>
     <methodResponse>
       <params>
     <param>
       <value>Alabama</value>
     </param>
       </params>
     </methodResponse>";
       HttpListenerContext context = _lstner.GetContext();
       switch (encoding)
       {
     case "gzip":
       context.Response.Headers.Add("Content-Encoding", "gzip");
       break;
     case "deflate":
       context.Response.Headers.Add("Content-Encoding", "deflate");
       break;
     default:
       break;
       }
       context.Response.ContentEncoding = System.Text.Encoding.UTF32;
       Stream respStm = context.Response.OutputStream;
       Stream compStm;
       switch (encoding)
       {
     case "gzip":
       compStm = new System.IO.Compression.GZipStream(respStm,
         System.IO.Compression.CompressionMode.Compress);
       break;
     case "deflate":
       compStm = new System.IO.Compression.DeflateStream(respStm,
         System.IO.Compression.CompressionMode.Compress);
       break;
     default:
       compStm = null;
       break;
       }
       StreamWriter wrtr = new StreamWriter(compStm);
       wrtr.Write(xml);
       wrtr.Close();
     }
       }
       catch (HttpListenerException ex)
       {
       }
 }
        public Exception Download(DownloadInfo downloadInfo)
        {
            HttpWebResponse response = null;
            try
            {
				downloadThread = System.Threading.Thread.CurrentThread;
                using (FileStream fs = new FileStream(downloadInfo.LocalFile, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    HttpWebRequest request = (HttpWebRequest)System.Net.WebRequest.Create(downloadInfo.Url);
                    request.Timeout = 15000;
                    request.UserAgent = OnlineVideoSettings.Instance.UserAgent;
                    request.Accept = "*/*";
                    request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
                    response = (HttpWebResponse)request.GetResponse();
                                        
                    Stream responseStream;
                    if (response.ContentEncoding.ToLower().Contains("gzip"))
                        responseStream = new System.IO.Compression.GZipStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);
                    else if (response.ContentEncoding.ToLower().Contains("deflate"))
                        responseStream = new System.IO.Compression.DeflateStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);
                    else
                        responseStream = response.GetResponseStream();

                    long size = response.ContentLength;
                    int buffSize = 4096;
                    byte[] buffer = new byte[buffSize];
                    long totalRead = 0;
                    long readSize;
                    do
                    {
                        readSize = responseStream.Read(buffer, 0, buffSize);
                        totalRead += readSize;
                        fs.Write(buffer, 0, (int)readSize);
                        downloadInfo.DownloadProgressCallback(size, totalRead);
                    }
                    while (readSize > 0 && !Cancelled);

                    fs.Flush();
                    fs.Close();

                    return null;
                }
            }
            catch (Exception ex)
            {
                return ex;
            }
            finally
            {
                if (response != null) response.Close();
            }
        }
Example #25
0
        private byte[] Compress(string data)
        {
            using (System.IO.MemoryStream mem = new System.IO.MemoryStream())
            {
                using (System.IO.Compression.GZipStream g = new System.IO.Compression.GZipStream(mem, System.IO.Compression.CompressionLevel.Optimal))
                {
                    byte[] bytes = System.Text.Encoding.UTF8.GetBytes(data);
                    g.Write(bytes, 0, bytes.Length);
                }

                return mem.ToArray();
            }
        }
Example #26
0
        private void start()
        {
            //Debug.Assert(MAPSIZE == 64, "The BlockBulkTransfer message requires a map size of 64.");

            for (byte x = 0; x < MAPSIZE; x++)
                for (byte y = 0; y < MAPSIZE; y += 16)
                {
                    NetBuffer msgBuffer = infsN.CreateBuffer();
                    msgBuffer.Write((byte)Infiniminer.InfiniminerMessage.BlockBulkTransfer);
                    if (!compression)
                    {
                        msgBuffer.Write(x);
                        msgBuffer.Write(y);
                        for (byte dy = 0; dy < 16; dy++)
                            for (byte z = 0; z < MAPSIZE; z++)
                                msgBuffer.Write((byte)(infs.blockList[x, y + dy, z]));
                        if (client.Status == NetConnectionStatus.Connected)
                            infsN.SendMessage(msgBuffer, client, NetChannel.ReliableUnordered);
                    }
                    else
                    {
                        //Compress the data so we don't use as much bandwith - Xeio's work
                        var compressedstream = new System.IO.MemoryStream();
                        var uncompressed = new System.IO.MemoryStream();
                        var compresser = new System.IO.Compression.GZipStream(compressedstream, System.IO.Compression.CompressionMode.Compress);

                        //Send a byte indicating that yes, this is compressed
                        msgBuffer.Write((byte)255);

                        //Write everything we want to compress to the uncompressed stream
                        uncompressed.WriteByte(x);
                        uncompressed.WriteByte(y);

                        for (byte dy = 0; dy < 16; dy++)
                            for (byte z = 0; z < MAPSIZE; z++)
                                uncompressed.WriteByte((byte)(infs.blockList[x, y + dy, z]));

                        //Compress the input
                        compresser.Write(uncompressed.ToArray(), 0, (int)uncompressed.Length);
                        //infs.ConsoleWrite("Sending compressed map block, before: " + uncompressed.Length + ", after: " + compressedstream.Length);
                        compresser.Close();

                        //Send the compressed data
                        msgBuffer.Write(compressedstream.ToArray());
                        if (client.Status == NetConnectionStatus.Connected)
                            infsN.SendMessage(msgBuffer, client, NetChannel.ReliableUnordered);
                    }
                }
            conn.Abort();
        }
Example #27
0
        public byte[] compress(byte[] data)
        {
            MemoryStream packed = new MemoryStream();

            System.IO.Stream packer = new System.IO.Compression.GZipStream(
                packed,
                System.IO.Compression.CompressionMode.Compress
            );

            packer.Write(data, 0, data.Length);
            packer.Close();

            return packed.ToArray();
        }
 protected override byte[] SerializeCore(object value, out long resultSize)
 {
     using (var ms = new MemoryStream())
     {
         using (var gzip = new System.IO.Compression.GZipStream(ms, compressionLevel))
         using (var sw = new StreamWriter(gzip))
         {
             new JsonSerializer().Serialize(sw, value);
         }
         var result = ms.ToArray();
         resultSize = result.Length;
         return result;
     }
 }
Example #29
0
        public static string lerror = ""; // Gets the last error that occurred in this struct. Similar to the C perror().

        #endregion Fields

        #region Methods

        public static bool compress(string infile, string outfile)
        {
            try {
                byte[] ifdata = System.IO.File.ReadAllBytes(infile);
                System.IO.StreamWriter sw = new System.IO.StreamWriter(outfile);
                System.IO.Compression.GZipStream gzs = new System.IO.Compression.GZipStream(sw.BaseStream, System.IO.Compression.CompressionMode.Compress);
                gzs.Write(ifdata, 0, ifdata.Length);
                gzs.Close();
                gzs.Dispose();
            } catch (System.Exception ex) {
                lerror = ex.Message;
                return false;
            }
            return true;
        }
Example #30
0
 /// <summary>
 /// Compress contents to gzip
 /// </summary>
 /// <param name="s"></param>
 /// <returns></returns>
 /// <remarks></remarks>
 public static byte[] GZIP(ref string s)
 {
     byte[] buffer = null;
     byte[] compressedData = null;
     MemoryStream oMemoryStream = null;
     System.IO.Compression.GZipStream compressedzipStream = null;
     oMemoryStream = new MemoryStream();
     buffer = System.Text.Encoding.UTF8.GetBytes(s);
     compressedzipStream = new System.IO.Compression.GZipStream(oMemoryStream, System.IO.Compression.CompressionMode.Compress, true);
     compressedzipStream.Write(buffer, 0, buffer.Length);
     compressedzipStream.Dispose();
     compressedzipStream.Close();
     compressedData = oMemoryStream.ToArray();
     oMemoryStream.Close();
     return compressedData;
 }
        public byte[] BuildPacket(byte[] dgram, TCPPacketFlags Flags)
        {
            DataStream OutDS   = null;
            DataStream InputDS = null;

            byte[] ret = null;
            try
            {
                OutDS   = new DataStream();
                InputDS = new DataStream();

                //build input dgram
                if ((Flags & TCPPacketFlags.GZipCompressed) > 0)
                {
                    InputDS.BW.Write((int)dgram.Length);

                    System.IO.Compression.GZipStream GZip = new System.IO.Compression.GZipStream(InputDS, System.IO.Compression.CompressionMode.Compress, true);
                    GZip.Write(dgram, 0, dgram.Length);
                    GZip.Close();
                    //versneltruck: kijk of de gecompressed versie wel echt kleiner is
                    if (InputDS.Length > dgram.Length)
                    {
                        //groter ==> geen compressie gebruiken

                        return(this.BuildPacket(dgram, Flags ^ TCPPacketFlags.GZipCompressed));
                    }
                }
                else
                {
                    InputDS.Write(dgram, 0, dgram.Length);
                }

                //header:
                OutDS.BW.Write((int)InputDS.Length);
                OutDS.BW.Write((byte)Flags);

                //content:
                InputDS.WriteTo(OutDS);

                ret = OutDS.ToArray();

                InputDS.Close();

                OutDS.Close();
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                if (InputDS != null)
                {
                    InputDS.Close();
                    InputDS = null;
                }
                if (OutDS != null)
                {
                    OutDS.Close();
                    OutDS = null;
                }
            }



            return(ret);
        }
Example #32
0
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <returns></returns>
        string _Send(Uri uri, string method)
        {
            if (IsMultipart)
            {
                method = "POST";
            }
            //HttpWebRequest request = null;
            if (String.IsNullOrEmpty(UserAgent) == false)
            {
                _WebClient.DefaultRequestHeaders.Add("User-Agent", UserAgent);
            }
            switch (method)
            {
            case "POST":

                using (System.IO.MemoryStream memory = new System.IO.MemoryStream())
                {
                    var writer = new System.IO.StreamWriter(memory);
                    if (IsMultipart)
                    {
                        for (int i = 0, l = this.PostData.Count; i < l; i++)
                        {
                            var name  = this.PostData.GetKey(i);
                            var value = this.PostData.Get(i);
                            writer.Write("--{0}{3}Content-Disposition: form-data; name=\"{1}\"{3}{3}{2}{3}",
                                         FormBoundary, name, value, _lineBreak);
                        }
                        for (int i = 0, l = this.Count; i < l; i++)
                        {
                            var name  = this.GetKey(i);
                            var value = this.BaseGet(i);
                            if (value is HttpFile)
                            {
                                var file     = value as HttpFile;
                                var filename = file.FileName ?? name;
                                var end      = filename.LastIndexOf('\\');
                                if (end == -1)
                                {
                                    end = filename.LastIndexOf('/');
                                }
                                if (end > 0)
                                {
                                    filename = filename.Substring(end + 1);
                                }
                                writer.Write("--{0}{4}Content-Disposition: form-data; name=\"{1}\"; filename=\"{2}\"{4}Content-Type: {3}{4}{4}",
                                             FormBoundary, name, filename, file.ContentType ?? "application/octet-stream", _lineBreak);
                                writer.Flush();
                                this.Writer(file, writer.BaseStream);
                                writer.Flush();
                                writer.Write(_lineBreak);
                            }
                            else
                            {
                                writer.Write("--{0}{3}Content-Disposition: form-data; name=\"{1}\"{3}{3}{2}{3}",
                                             FormBoundary, name, this[i], _lineBreak);
                            }
                        }
                        writer.Write("--{0}--", FormBoundary, _lineBreak);
                    }
                    else
                    {
                        //_WebClient.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded; charset=UTF-8";
                        Escape(writer, this.PostData, true);
                        Escape(writer, this, this.PostData.Count == 0);
                    }

                    writer.Flush();
                    memory.Position = 0;
                    var cont = new System.Net.Http.StreamContent(memory);
                    cont.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse(this.IsMultipart ? GetMultipartFormContentType() : "application/x-www-form-urlencoded; charset=UTF-8");
                    //_WebClient.PostAsync()

                    var httpContent = _WebClient.PostAsync(uri, cont).Result.Content;
                    if (httpContent.Headers.ContentEncoding.ToString() == "gzip")
                    {
                        var zip = new System.IO.Compression.GZipStream(httpContent.ReadAsStreamAsync().Result
                                                                       , System.IO.Compression.CompressionMode.Decompress);

                        return(GetResult(zip));
                    }
                    else
                    {
                        return(httpContent.ReadAsStringAsync().Result);
                    }
                }

            default:
                var sb  = new StringBuilder();
                var wsb = new System.IO.StringWriter(sb);
                Escape(wsb, this.PostData, true);
                Escape(wsb, this, this.PostData.Count == 0);
                //  var url = this.GetUrl();
                if (sb.Length > 0)
                {
                    if (String.IsNullOrEmpty(uri.Query))
                    {
                        sb.Insert(0, '?');
                    }
                    else
                    {
                        sb.Insert(0, '&');
                    }
                }
                sb.Insert(0, uri.AbsoluteUri);


                var httpContent2 = _WebClient.GetAsync(sb.ToString()).Result.Content;
                if (httpContent2.Headers.ContentEncoding.ToString() == "gzip")
                {
                    var zip = new System.IO.Compression.GZipStream(httpContent2.ReadAsStreamAsync().Result
                                                                   , System.IO.Compression.CompressionMode.Decompress);

                    return(GetResult(zip));
                }
                else
                {
                    return(httpContent2.ReadAsStringAsync().Result);
                }
            }
        }
Example #33
0
        public static System.IO.Compression.GZipStream GzToStream(FileStream gzfile)
        {
            var gzstream = new System.IO.Compression.GZipStream(gzfile, System.IO.Compression.CompressionMode.Decompress);

            return(gzstream);
        }
        public static string GetWebData(string url, CookieContainer cc = null, string referer = null, IWebProxy proxy = null, bool forceUTF8 = false, bool allowUnsafeHeader = false, string userAgent = null, Encoding encoding = null)
        {
            HttpWebResponse response = null;

            try
            {
                string requestCRC = Helpers.EncryptionUtils.CalculateCRC32(string.Format("{0}{1}{2}{3}{4}", url, referer, userAgent, proxy != null ? proxy.GetProxy(new Uri(url)).AbsoluteUri : "", cc != null ? cc.GetCookieHeader(new Uri(url)) : ""));

                // try cache first
                string cachedData = WebCache.Instance[requestCRC];
                Log.Debug("GetWebData{1}: '{0}'", url, cachedData != null ? " (cached)" : "");
                if (cachedData != null)
                {
                    return(cachedData);
                }

                // request the data
                if (allowUnsafeHeader)
                {
                    Helpers.DotNetFrameworkHelper.SetAllowUnsafeHeaderParsing(true);
                }
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                if (request == null)
                {
                    return("");
                }
                if (!String.IsNullOrEmpty(userAgent))
                {
                    request.UserAgent = userAgent; // set specific UserAgent if given
                }
                else
                {
                    request.UserAgent = OnlineVideoSettings.Instance.UserAgent;        // set OnlineVideos default UserAgent
                }
                request.Accept = "*/*";                                                // we accept any content type
                request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate"); // we accept compressed content

                request.Headers.Add("X-Requested-With: XMLHttpRequest");
                request.Headers.Add("x-addr: 127.0.0.1");

                if (!String.IsNullOrEmpty(referer))
                {
                    request.Referer = referer;                                 // set referer if given
                }
                if (cc != null)
                {
                    request.CookieContainer = cc;             // set cookies if given
                }
                if (proxy != null)
                {
                    request.Proxy = proxy;                // send the request over a proxy if given
                }
                try
                {
                    response = (HttpWebResponse)request.GetResponse();
                }
                catch (WebException webEx)
                {
                    Log.Debug(webEx.Message);
                    response = (HttpWebResponse)webEx.Response; // if the server returns a 404 or similar .net will throw a WebException that has the response
                }
                Stream responseStream;
                if (response == null)
                {
                    return("");
                }
                if (response.ContentEncoding.ToLower().Contains("gzip"))
                {
                    responseStream = new System.IO.Compression.GZipStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);
                }
                else if (response.ContentEncoding.ToLower().Contains("deflate"))
                {
                    responseStream = new System.IO.Compression.DeflateStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);
                }
                else
                {
                    responseStream = response.GetResponseStream();
                }

                // UTF8 is the default encoding as fallback
                Encoding responseEncoding = Encoding.UTF8;
                // try to get the response encoding if one was specified and neither forceUTF8 nor encoding were set as parameters
                if (!forceUTF8 && encoding == null && response.CharacterSet != null && !String.IsNullOrEmpty(response.CharacterSet.Trim()))
                {
                    responseEncoding = Encoding.GetEncoding(response.CharacterSet.Trim(new char[] { ' ', '"' }));
                }
                // the caller did specify a forced encoding
                if (encoding != null)
                {
                    responseEncoding = encoding;
                }
                // the caller wants to force UTF8
                if (forceUTF8)
                {
                    responseEncoding = Encoding.UTF8;
                }

                using (StreamReader reader = new StreamReader(responseStream, responseEncoding, true))
                {
                    string str = reader.ReadToEnd().Trim();
                    // add to cache if HTTP Status was 200 and we got more than 500 bytes (might just be an errorpage otherwise)
                    if (response.StatusCode == HttpStatusCode.OK && str.Length > 500)
                    {
                        WebCache.Instance[requestCRC] = str;
                    }
                    return(str);
                }
            }
            finally
            {
                if (response != null)
                {
                    ((IDisposable)response).Dispose();
                }
                // disable unsafe header parsing if it was enabled
                if (allowUnsafeHeader)
                {
                    Helpers.DotNetFrameworkHelper.SetAllowUnsafeHeaderParsing(false);
                }
            }
        }
Example #35
0
        /// <summary>
        /// GZIPを解凍するサンプル
        /// </summary>
        /// <param name="args">未使用</param>
        static void Main(string[] args)
        {
            try
            {
                Console.Write("解凍するGZIPファイルのパス:");
                var targetFilePath = Console.ReadLine();

                if (targetFilePath.EndsWith(".gz") == false)
                {
                    Console.WriteLine("GZIP以外のファイルが指定されています。");
                    return;
                }

                var decompressedFilePath = targetFilePath.Replace(".gz", "");
                Console.WriteLine("GZIP解凍後のファイルパスは {0} です。", decompressedFilePath);

                if (System.IO.File.Exists(targetFilePath) == false)
                {
                    Console.WriteLine("指定したファイル {0} が見付かりませんでした。", targetFilePath);
                    return;
                }

                //解凍対象ファイル読取ストリームを生成する。
                var readFileStream = new System.IO.FileStream(targetFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                //GZIP解凍ストリームを生成する。
                var decompressionStream = new System.IO.Compression.GZipStream(readFileStream, System.IO.Compression.CompressionMode.Decompress);
                //解凍ファイル出力ストリームを生成する。
                var writeFileStream = new System.IO.FileStream(decompressedFilePath, System.IO.FileMode.Create);

                using (readFileStream)
                    using (decompressionStream)
                        using (writeFileStream)
                        {
                            //1KBずつ順次書き出す。
                            var num = 0;
                            var buf = new byte[1024];
                            while ((num = decompressionStream.Read(buf, 0, buf.Length)) > 0)
                            {
                                writeFileStream.Write(buf, 0, num);
                            }

                            //一撃で解凍しようとしても解凍後のByte数が分からないため、例えば下記処理では正しく書き出されない。
                            ////解凍対象ファイルをバイト配列で取得する。
                            //var fileBytes = System.IO.File.ReadAllBytes(targetFilePath);
                            ////解凍対象ファイルのバイト配列分のバイト配列を生成する。
                            //var decompressedBytes = new byte[fileBytes.Length];
                            ////解答する。
                            //var readByteCount = decompressionStream.Read(decompressedBytes, 0, fileBytes.Length);
                            ////解凍したファイルを出力する。
                            //writeFileStream.Write(decompressedBytes, 0, readByteCount);
                        }

                Console.WriteLine("完了しました!");
            }
            catch (Exception ex)
            {
                Console.WriteLine(Environment.NewLine + ex.ToString());
            }
            finally
            {
                Console.ReadLine();
            }
        }
Example #36
0
        /// <summary>
        /// 已登录持有cookie进行post
        /// </summary>
        /// <param name="postUrl"></param>
        /// <param name="referUrl"></param>
        /// <param name="data"></param>
        /// <param name="cookiec"></param>
        /// <returns></returns>
        public string PostData(string postUrl, string referUrl, string data, CookieContainer cookiec)
        {
            //data = System.Web.HttpUtility.UrlEncode(data);
            string result = "";

            try
            {
                HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(postUrl);
                myHttpWebRequest.AllowAutoRedirect = true;
                myHttpWebRequest.KeepAlive         = false;
                myHttpWebRequest.Accept            = "*/*";
                myHttpWebRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
                myHttpWebRequest.CookieContainer = cookiec;
                myHttpWebRequest.UserAgent       = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 2.0.50727)";
                myHttpWebRequest.ContentType     = "application/x-www-form-urlencoded; charset=UTF-8";
                myHttpWebRequest.Referer         = referUrl;
                myHttpWebRequest.Method          = "POST";
                myHttpWebRequest.Timeout         = 80000;
                myHttpWebRequest.ContentLength   = data.Length;
                Stream postStream = myHttpWebRequest.GetRequestStream();
                byte[] postData   = Encoding.UTF8.GetBytes(data);
                postStream.Write(postData, 0, postData.Length);
                postStream.Dispose();
                HttpWebResponse response = (HttpWebResponse)myHttpWebRequest.GetResponse();
                response.Cookies = cookiec.GetCookies(myHttpWebRequest.RequestUri);
                Stream streamReceive;
                string gzip = response.ContentEncoding;
                if (string.IsNullOrEmpty(gzip) || gzip.ToLower() != "gzip")
                {
                    streamReceive = response.GetResponseStream();
                }
                else
                {
                    streamReceive = new System.IO.Compression.GZipStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);
                }
                StreamReader sr = new System.IO.StreamReader(streamReceive, Encoding.UTF8);
                if (response.ContentLength > 1)
                {
                    result = sr.ReadToEnd();
                }
                else
                {
                    char[]        buffer = new char[256];
                    int           count  = 0;
                    StringBuilder sb     = new StringBuilder();
                    while ((count = sr.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        sb.Append(new string(buffer));
                    }
                    result = sb.ToString();
                }
                sr.Close();
                response.Close();
                string s = result;
                return(s);
            }
            catch { }
            finally
            {
                Console.WriteLine("!!!!!!!!!");
            }
            return("");
        }
Example #37
0
        /// <summary>
        /// 发布评论
        /// </summary>
        /// <param name="PostNo">纯数字ac号</param>
        /// <param name="data"></param>
        public string comment(string PostNo, string data, CookieContainer cookiec)
        {
            //posturl
            //http://www.acfun.tv/comment.aspx
            //data:
            //name:sendComm()
            //token:mimiko
            //quoteId:0
            //text:%E8%A6%81%E9%82%A3%E4%B9%88%E5%A4%9A%E7%9F%B3%E6%B2%B9%E5%B9%B2%E5%98%9B%EF%BC%8C%E5%8D%96%E4%BA%86%E5%8D%96%E4%BA%86
            //cooldown:5000
            //contentId:1832378
            //name=sendComm()&token=mimiko&quoteId=0&text=%E8%A6%81%E9%82%A3%E4%B9%88%E5%A4%9A%E7%9F%B3%E6%B2%B9%E5%B9%B2%E5%98%9B%EF%BC%8C%E5%8D%96%E4%BA%86%E5%8D%96%E4%BA%86&cooldown=5000&contentId=1832378
            //refer
            //http://www.acfun.tv/a/ac1832378
            data = System.Web.HttpUtility.UrlEncode(data);
            data = "name=sendComm()&token=mimiko&quoteId=0&text=" + data + "&cooldown=5000&contentId=" + PostNo + "&quoteName=";
            string result = "";

            try
            {
                HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://www.acfun.tv/comment.aspx");
                myHttpWebRequest.AllowAutoRedirect = true;
                myHttpWebRequest.KeepAlive         = true;
                myHttpWebRequest.Accept            = "*/*";
                myHttpWebRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
                myHttpWebRequest.CookieContainer = cookiec;
                myHttpWebRequest.UserAgent       = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 2.0.50727)";
                myHttpWebRequest.ContentType     = "application/x-www-form-urlencoded; charset=UTF-8";
                myHttpWebRequest.Referer         = "http://www.acfun.tv/a/ac" + PostNo;
                myHttpWebRequest.Method          = "POST";
                myHttpWebRequest.Timeout         = 100000;
                myHttpWebRequest.ContentLength   = data.Length;
                Stream postStream = myHttpWebRequest.GetRequestStream();
                byte[] postData   = Encoding.UTF8.GetBytes(data);
                postStream.Write(postData, 0, postData.Length);
                postStream.Dispose();
                HttpWebResponse response = (HttpWebResponse)myHttpWebRequest.GetResponse();
                response.Cookies = cookiec.GetCookies(myHttpWebRequest.RequestUri);
                Stream streamReceive;
                string gzip = response.ContentEncoding;
                if (string.IsNullOrEmpty(gzip) || gzip.ToLower() != "gzip")
                {
                    streamReceive = response.GetResponseStream();
                }
                else
                {
                    streamReceive = new System.IO.Compression.GZipStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);
                }
                StreamReader sr = new System.IO.StreamReader(streamReceive, Encoding.UTF8);
                if (response.ContentLength > 1)
                {
                    result = sr.ReadToEnd();
                }
                else
                {
                    char[]        buffer = new char[256];
                    int           count  = 0;
                    StringBuilder sb     = new StringBuilder();
                    while ((count = sr.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        sb.Append(new string(buffer));
                    }
                    result = sb.ToString();
                }
                sr.Close();
                response.Close();
                string s = result;
                return(s);
            }
            catch (Exception)
            {
                //throw;
            }
            finally
            {
                Console.WriteLine("!!!!!!!!!");
            }
            return("");
        }
Example #38
0
        /// <summary>
        /// 获取 Url 的相应 html
        /// </summary>
        /// <param name="url"></param>
        /// <param name="timeout">超时毫秒</param>
        /// <returns></returns>
        public static string GetHTML(string url, int timeout)
        {
            HttpWebRequest  request  = null;
            HttpWebResponse response = null;
            StreamReader    reader   = null;

            try
            {
                request           = (HttpWebRequest)WebRequest.Create(url);
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";

                if (timeout > 0)
                {
                    request.Timeout = timeout;
                }

                request.AllowAutoRedirect = true;

                response = (HttpWebResponse)request.GetResponse();

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    string CharSet = response.CharacterSet;

                    #region 获取目标页面的字符集

                    if (string.IsNullOrEmpty(CharSet) || (CharSet == "ISO-8859-1"))
                    {
                        string head  = response.Headers["Content-Type"];
                        Regex  regex = new Regex(@"charset=[^""]?[""](?<G0>([^""]+?))[""]", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                        Match  m     = regex.Match(head);

                        if (m.Success)
                        {
                            CharSet = m.Groups["G0"].Value;
                        }
                    }

                    if (CharSet == "ISO-8859-1")
                    {
                        CharSet = "GB2312";
                    }
                    if (string.IsNullOrEmpty(CharSet))
                    {
                        CharSet = "UTF-8";
                    }

                    #endregion

                    Stream s = null;

                    if (response.ContentEncoding.ToLower() == "gzip")
                    {
                        s = new System.IO.Compression.GZipStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);
                    }
                    else if (response.ContentEncoding.ToLower() == "deflate")
                    {
                        s = new System.IO.Compression.DeflateStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);
                    }
                    else
                    {
                        s = response.GetResponseStream();
                    }

                    reader = new StreamReader(s, System.Text.Encoding.GetEncoding(CharSet));
                    string html = reader.ReadToEnd();
                    return(html);
                }
                else
                {
                    return("");
                }
            }
            catch (SystemException)
            {
                return("");
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }

                if (response != null)
                {
                    response.Close();
                }
            }
        }
Example #39
0
        /// <summary>
        /// 获取 Url 的相应 html
        /// </summary>
        /// <param name="url"></param>
        /// <param name="type"></param>
        /// <param name="lastModifiedTime"></param>
        /// <param name="hostAbsolutePath"></param>
        /// <returns></returns>
        public static string GetHTML(string url, ref int type, ref System.DateTime lastModifiedTime, ref string hostAbsolutePath)
        {
            type             = -1;
            lastModifiedTime = System.DateTime.Now;
            hostAbsolutePath = "";

            HttpWebRequest  request;
            HttpWebResponse response = null;
            Stream          s        = null;
            StreamReader    sr       = null;

            bool ReadOK = false;

            try
            {
                request                   = (HttpWebRequest)WebRequest.Create(url);
                request.UserAgent         = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
                request.Timeout           = 30000;
                request.AllowAutoRedirect = true;

                response = (HttpWebResponse)request.GetResponse();

                if (response.StatusCode != HttpStatusCode.OK)
                {
                    type = -1;
                    return("");
                }

                lastModifiedTime = response.LastModified;
                hostAbsolutePath = response.ResponseUri.AbsoluteUri;
                string Path    = response.ResponseUri.AbsolutePath;
                int    iLocate = Path.LastIndexOf("/", StringComparison.Ordinal);
                if (iLocate > 0)
                {
                    Path = Path.Substring(iLocate, Path.Length - iLocate);
                }
                hostAbsolutePath = hostAbsolutePath.Substring(0, hostAbsolutePath.Length - Path.Length).ToLower();

                if (response.ContentType.ToLower().StartsWith("image/", StringComparison.Ordinal))      //是图片
                {
                    type = 2;
                    return("");
                }

                if (response.ContentType.ToLower().StartsWith("audio/", StringComparison.Ordinal))      //是声音
                {
                    type = 3;
                    return("");
                }

                if (!response.ContentType.ToLower().StartsWith("text/", StringComparison.Ordinal))      //不是文本类型的文档
                {
                    type = -1;
                    return("");
                }

                string CharSet = response.CharacterSet;

                #region 获取目标页面的字符集

                if (string.IsNullOrEmpty(CharSet) || (CharSet == "ISO-8859-1"))
                {
                    string head  = response.Headers["Content-Type"];
                    Regex  regex = new Regex(@"charset=[^""]?[""](?<G0>([^""]+?))[""]", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                    Match  m     = regex.Match(head);

                    if (m.Success)
                    {
                        CharSet = m.Groups["G0"].Value;
                    }
                }

                if (CharSet == "ISO-8859-1")
                {
                    CharSet = "GB2312";
                }
                if (string.IsNullOrEmpty(CharSet))
                {
                    CharSet = "UTF-8";
                }

                #endregion

                if (response.ContentEncoding.ToLower() == "gzip")
                {
                    s = new System.IO.Compression.GZipStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);
                }
                else if (response.ContentEncoding.ToLower() == "deflate")
                {
                    s = new System.IO.Compression.DeflateStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);
                }
                else
                {
                    s = response.GetResponseStream();
                }

                sr = new StreamReader(s, System.Text.Encoding.GetEncoding(CharSet));

                ReadOK = true;
            }
            catch
            {
                type = -1;
                return("");
            }
            finally
            {
                if (!ReadOK)
                {
                    if (sr != null)
                    {
                        sr.Close();
                    }

                    if (s != null)
                    {
                        s.Close();
                    }

                    if (response != null)
                    {
                        response.Close();
                    }
                }
            }

            string HTML  = "";
            string sLine = "";

            try
            {
                while (sLine != null)
                {
                    sLine = sr.ReadLine();
                    if (sLine != null)
                    {
                        HTML += sLine;
                    }
                }
                type = 1;
                return(HTML);
            }
            catch
            {
                type = -1;
                return("");
            }
            finally
            {
                if (sr != null)
                {
                    sr.Close();
                }

                if (s != null)
                {
                    s.Close();
                }

                if (response != null)
                {
                    response.Close();
                }
            }
        }
Example #40
0
        /// <summary>
        /// 功能描述:在PostLogin成功登录后记录下Headers中的cookie,然后获取此网站上其他页面的内容
        /// </summary>
        /// <param name="strURL">获取网站的某页面的地址</param>
        /// <param name="strReferer">引用的地址</param>
        /// <returns>返回页面内容</returns>
        public string GetPage(string strURL, string strReferer, CookieContainer cookiec)
        {
            string strResult = "";

            try
            {
                HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(strURL);
                myHttpWebRequest.AllowAutoRedirect = true;
                myHttpWebRequest.KeepAlive         = true;
                myHttpWebRequest.Accept            = "*/*";
                myHttpWebRequest.Headers.Add("Accept-Encoding", "gzip, deflate ");
                myHttpWebRequest.CookieContainer = cookiec;
                myHttpWebRequest.UserAgent       = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 2.0.50727)";
                myHttpWebRequest.ContentType     = "application/json;charset=UTF-8";
                myHttpWebRequest.Method          = "GET";
                myHttpWebRequest.Timeout         = 300000;
                HttpWebResponse        response = null;
                System.IO.StreamReader sr       = null;
                BinaryReader           br       = null;
                response = (HttpWebResponse)myHttpWebRequest.GetResponse();
                if (cookiec != null)
                {
                    response.Cookies = cookiec.GetCookies(myHttpWebRequest.RequestUri);
                }
                Stream streamReceive;
                string gzip = response.ContentEncoding;
                if (string.IsNullOrEmpty(gzip) || gzip.ToLower() != "gzip")
                {
                    streamReceive = response.GetResponseStream();
                }
                else
                {
                    streamReceive = new System.IO.Compression.GZipStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);
                }
                sr = new System.IO.StreamReader(streamReceive, Encoding.UTF8);
                br = new System.IO.BinaryReader(streamReceive, Encoding.UTF8);
                //if (response.ContentLength > 1)
                //{
                strResult = sr.ReadToEnd();
                //}
                //else
                //{
                //    char[] buffer = new char[64];
                //    int count = 0;
                //    StringBuilder sb = new StringBuilder();
                //    while ((count = br.Read(buffer, 0, buffer.Length)) > 0)
                //    {
                //        sb.Append(new string(buffer));
                //    }
                //    strResult = sb.ToString();
                //}
                sr.Close();
                response.Close();
            }
            catch (Exception e)
            {
                //throw;
            }
            finally
            {
                Console.WriteLine("!!!!!!!!!");
            }
            return(strResult);
        }
Example #41
0
        public void ResetToStart(Action <Stream> AfterInit)
        {
            try
            {
                // in case the stream is at the beginning do nothing
                if (Stream != null && Stream.CanSeek)
                {
                    Stream.Position = 0;
                }
                else
                {
                    if (Stream != null)
                    {
                        Stream.Close();
                        // need to reopen the base stream
                        BaseStream = File.OpenRead(BasePath);
                    }

                    if (AssumeGZip)
                    {
                        Log.Debug("Decompressing GZip Stream");
                        Stream = new System.IO.Compression.GZipStream(BaseStream, System.IO.Compression.CompressionMode.Decompress);
                    }

                    else if (AssumePGP)
                    {
                        System.Security.SecureString DecryptedPassphrase = null;
                        // need to use the setting function, opening a form to enter the passphrase is not in this library
                        if (string.IsNullOrEmpty(EncryptedPassphrase))
                        {
                            throw new ApplicationException("Please provide a passphrase.");
                        }
                        try
                        {
                            DecryptedPassphrase = EncryptedPassphrase.Decrypt().ToSecureString();
                        }
                        catch (Exception)
                        {
                            throw new ApplicationException("Please reenter the passphrase, the passphrase could not be decrypted.");
                        }

                        try
                        {
                            Log.Debug("Decrypt PGP Stream");
                            Stream = ApplicationSetting.ToolSetting.PGPInformation.PgpDecrypt(BaseStream, DecryptedPassphrase);
                        }
                        catch (Org.BouncyCastle.Bcpg.OpenPgp.PgpException ex)
                        {
                            // removed possibly stored passphrase
                            var recipinet = string.Empty;
                            try
                            {
                                recipinet = ApplicationSetting.ToolSetting?.PGPInformation?.GetEncryptedKeyID(BaseStream);
                            }
                            catch
                            {
                                // ignore
                            }

                            if (recipinet.Length > 0)
                            {
                                throw new ApplicationException($"The message is encrypted for '{recipinet}'.", ex);
                            }
                            else
                            {
                                throw;
                            }
                        }
                    }
                    else
                    {
                        Stream = BaseStream;
                    }
                }
            }
            finally
            {
                AfterInit?.Invoke(Stream);
            }
        }
        internal void AssembleAndClose()
        {
            //TODO release all file handles and flush data to disk and move file from cache to server/port directory
            this.Close();

            string fixedFilename     = this.FilePath;//no directory info
            string fixedFileLocation = "";

            FileStreamAssembler.FixFilenameAndLocation(ref fixedFilename, ref fixedFileLocation);

            string destinationPath;
            Uri    relativeUri;

            //reassemble the files at the server, regardless if they were downloaded from there or uploaded to the server
            if (this.transferIsClientToServer)
            {
                (destinationPath, relativeUri) = FileStreamAssembler.GetFilePath(FileStreamAssembler.FileAssmeblyRootLocation.destination, this.fiveTuple, this.transferIsClientToServer, this.fileStreamType, fixedFileLocation, fixedFilename, this.fileStreamAssemblerList, "");
            }
            else
            {
                (destinationPath, relativeUri) = FileStreamAssembler.GetFilePath(FileStreamAssembler.FileAssmeblyRootLocation.source, this.fiveTuple, this.transferIsClientToServer, this.fileStreamType, fixedFileLocation, fixedFilename, this.fileStreamAssemblerList, "");
            }


            //I need to create the directory here since the file might either be moved to this located or a new file will be created there from a stream
            //string directoryName = destinationPath.Substring(0, destinationPath.Length - fixedFilename.Length);
            string directoryName = System.IO.Path.GetDirectoryName(destinationPath);

            if (!System.IO.Directory.Exists(directoryName))
            {
                try {
                    System.IO.Directory.CreateDirectory(directoryName);
                }
                catch (Exception e) {
                    this.fileStreamAssemblerList.PacketHandler.OnAnomalyDetected("Error creating directory \"" + directoryName + "\" for path \"" + destinationPath + "\".\n" + e.Message);
                }
            }


            //files which are already completed can simply be moved to their final destination
            if (this.fileStream != null)
            {
                this.fileStream.Close();
            }
            try {
                //string tmpPath = this.tempFilePath;
                if (System.IO.File.Exists(this.tempFilePath))
                {
                    if (this.ContentEncoding == "gzip")
                    {
                        using (System.IO.FileStream compressedStream = new System.IO.FileStream(this.tempFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read)) {
                            using (System.IO.Compression.GZipStream decompressedStream = new System.IO.Compression.GZipStream(compressedStream, System.IO.Compression.CompressionMode.Decompress)) {
                                using (System.IO.FileStream destinationStream = new System.IO.FileStream(destinationPath, System.IO.FileMode.CreateNew)) {
                                    decompressedStream.CopyTo(destinationStream);
                                }
                            }
                        }
                    }
                    else
                    {
                        System.IO.File.Move(this.tempFilePath, destinationPath);
                    }
                }
            }
            catch (Exception e) {
                this.fileStreamAssemblerList.PacketHandler.OnAnomalyDetected("Error moving file \"" + this.tempFilePath + "\" to \"" + destinationPath + "\". " + e.Message);
            }

            if (System.IO.File.Exists(destinationPath))
            {
                try {
                    ReconstructedFile completedFile = new ReconstructedFile(destinationPath, relativeUri, this.fiveTuple, this.transferIsClientToServer, this.fileStreamType, this.details, this.initialFrameNumber, this.initialTimeStamp, this.serverHostname);
                    this.fileStreamAssemblerList.PacketHandler.AddReconstructedFile(completedFile);
                    //parentAssemblerList.PacketHandler.ParentForm.ShowReconstructedFile(completedFile);
                }
                catch (Exception e) {
                    this.fileStreamAssemblerList.PacketHandler.OnAnomalyDetected("Error creating reconstructed file: " + e.Message);
                }
            }
        }
        public async Task <HttpResponseMessage> Run(CancellationToken cancel)
        {
            var log = Configuration.Services.GetTraceWriter();
            HttpRequestMessage req = Request;

            log.Info(req, "Query", "Request Begin request.");

            //don't support duplicates of query string parameters
            var queryString = req.GetQueryNameValuePairs().ToDictionary(p => p.Key, p => p.Value);

            //var server = @"asazure://southcentralus.asazure.windows.net/dbrowneaas";


            var authData = new AuthData(req);

            if (authData.Scheme == AuthScheme.NONE)
            {
                var challengeResponse = req.CreateErrorResponse(HttpStatusCode.Unauthorized, "Unauthorized");
                challengeResponse.Headers.WwwAuthenticate.Add(new AuthenticationHeaderValue("Bearer", @"realm=""*.asazure.windows.net"""));
                if (req.RequestUri.Scheme == "https")
                {
                    challengeResponse.Headers.WwwAuthenticate.Add(new AuthenticationHeaderValue("Basic", @"realm=""*.asazure.windows.net"""));
                }
                return(challengeResponse);
            }

            if (authData.Scheme == AuthScheme.BASIC && req.RequestUri.Scheme == "http")
            {
                return(req.CreateErrorResponse(HttpStatusCode.BadRequest, "HTTP Basic Auth only supported with https requests."));
            }



            var server   = ConfigurationManager.AppSettings["Server"];
            var database = ConfigurationManager.AppSettings["Database"];

            if (string.IsNullOrEmpty(server))
            {
                throw new InvalidOperationException("Required AppSettings Server is missing.");
            }
            if (string.IsNullOrEmpty(database))
            {
                throw new InvalidOperationException("Required AppSettings Database is missing.");
            }

            string constr;

            if (authData.Scheme == AuthScheme.BASIC)
            {
                constr = $"Data Source={server};User Id={authData.UPN};Password={authData.PasswordOrToken};Catalog={database};Persist Security Info=True; Impersonation Level=Impersonate";
            }
            else if (authData.Scheme == AuthScheme.BEARER)
            {
                constr = $"Data Source={server};Password={authData.PasswordOrToken};Catalog={database};Persist Security Info=True; Impersonation Level=Impersonate";
            }
            else
            {
                throw new InvalidOperationException($"unexpected state authData.Scheme={authData.Scheme}");
            }

            //get gzip setting
            bool gzip = queryString.ContainsKey("gzip") ? bool.Parse(queryString["gzip"]) : true;
            //if (req.Headers.AcceptEncoding.Any(h => h.Value == "gzip" || h.Value == "*"))
            //{
            //    gzip = true;
            //}

            string query;

            if (req.Method == HttpMethod.Get)
            {
                if (!queryString.ContainsKey("query"))
                {
                    return(req.CreateErrorResponse(HttpStatusCode.BadRequest, "get request must include 1 'query' query string parameter", new ArgumentException("query")));
                }
                query = queryString["query"];
            }
            else
            {
                query = await req.Content.ReadAsStringAsync();
            }

            var con = ConnectionPool.Instance.GetConnection(constr, authData);

            var cmd = con.Connection.CreateCommand();

            cmd.CommandText = query;

            object queryResults;

            try
            {
                cmd.CommandTimeout = 2 * 60;
                cancel.Register(() =>
                {
                    cmd.Cancel();
                    con.Connection.Dispose();
                    log.Info(Request, "Query", "Query Execution Canceled");
                });
                queryResults = cmd.Execute();
            }
            catch (Exception ex)
            {
                return(req.CreateErrorResponse(HttpStatusCode.InternalServerError, ex));
            }

            var resp = (HttpResponseMessage)req.CreateResponse();

            resp.StatusCode = HttpStatusCode.OK;

            var streaming = true;

            //var indent = true;


            resp.Content = new PushStreamContent(async(responseStream, content, transportContext) =>
            {
                try
                {
                    System.IO.Stream encodingStream = responseStream;
                    if (gzip)
                    {
                        encodingStream = new System.IO.Compression.GZipStream(responseStream, System.IO.Compression.CompressionMode.Compress, false);
                    }

                    using (responseStream)
                        using (encodingStream)
                        {
                            if (streaming)
                            {
                                await ResultWriter.WriteResultsToStream(queryResults, encodingStream, cancel);
                            }
                            else
                            {
                                var ms = new MemoryStream();
                                await ResultWriter.WriteResultsToStream(queryResults, ms, cancel);
                                ms.Position = 0;
                                var buf     = new byte[256];
                                ms.Read(buf, 0, buf.Length);
                                var str = System.Text.Encoding.UTF8.GetString(buf);
                                log.Info(Request, "Query", $"buffered query results starting {str}");
                                ms.Position = 0;

                                ms.CopyTo(encodingStream);
                            }

                            ConnectionPool.Instance.ReturnConnection(con);
                            await encodingStream.FlushAsync();
                            await responseStream.FlushAsync();
                        }
                }
                catch (Exception ex)
                {
                    log.Error(Request, "Query", ex);
                    con.Connection.Dispose();//do not return to pool
                    throw;
                }
            }, "application/json");
            if (gzip)
            {
                resp.Content.Headers.ContentEncoding.Add("gzip");
            }

            return(resp);
        }
        /// <summary>
        /// Auth: user.snh48.com
        /// Request Type: GET
        /// URL format: http://user.snh48.com/vip48.php?callback=jQuery18006247164210821841_1479118420647&username=yukanana7&password=angel0416&act=login&_=1479118433288
        /// </summary>
        /// <returns>bool</returns>
        protected bool UserSnh48Com_Vip48Php_Auth()
        {
            // Main Entry Request - GET
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.ps_MainEntryUrl);

            request.Referer   = ps_StartPointReferer;
            request.UserAgent = ps_UserAgent;
            request.KeepAlive = true;
            request.Method    = "GET";
            request.Accept    = "*/*";
            request.Headers.Add("Accept-Language", ps_AcceptLanguage);
            request.Headers.Add("Accept-Encoding", ps_AcceptEncoding);

            // Main Entry Response
            bool            result   = false;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            if (response.StatusCode == HttpStatusCode.OK)
            {
                Stream streamReceive;
                string stringContainer;
                System.IO.Compression.GZipStream zipStream;

                // if ContentEncoding is gzip, need to de-compress
                if (response.ContentEncoding.ToLower() == "gzip")
                {
                    streamReceive   = response.GetResponseStream();
                    zipStream       = new System.IO.Compression.GZipStream(streamReceive, System.IO.Compression.CompressionMode.Decompress);
                    stringContainer = new StreamReader(zipStream, Encoding.Default).ReadToEnd().Trim();
                }
                else
                {
                    stringContainer = new StreamReader(response.GetResponseStream(), Encoding.Default).ReadToEnd().Trim();
                }

                // substring 1 -> stringContainer`s size - 2
                // purpose: exclude the "(" at first and ")" at the end of the response.
                string responseString = stringContainer.Substring(1, stringContainer.Length - 2);

                // Deserialize to hashtable
                JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
                object o = jsSerializer.Deserialize(responseString, typeof(Hashtable));

                // desc value
                Hashtable jsonResult           = o as Hashtable;
                string    endPointBlocksString = jsonResult["desc"].ToString().Trim();

                // Get endPointsUri
                string startTag = "src=";
                string endTag   = "reload=";

                int startIndex = endPointBlocksString.IndexOf(startTag);
                int endIndex   = endPointBlocksString.IndexOf(endTag);

                while (startIndex > 0 && endIndex > 0 &&
                       startIndex < endPointBlocksString.Length && endIndex < endPointBlocksString.Length)
                {
                    int len      = endIndex - 2 - (startIndex + startTag.Length + 1);
                    Uri endpoint = new Uri(endPointBlocksString.Substring(startIndex + startTag.Length + 1, len));
                    authEndPoints.Add(endpoint);

                    // shop48cn endpoint auth Uri
                    if (endpoint.AbsoluteUri.Contains(ps_shop48cn))
                    {
                        this.puri_shop48cnAuthUrl = endpoint;
                        result = true;
                    }
                    else if (endpoint.AbsoluteUri.Contains(ps_www48cn))
                    {
                        this.puri_www48cnAuthUrl = endpoint;
                    }

                    // Move to the next Uri
                    startIndex = endPointBlocksString.IndexOf(startTag, endIndex);
                    if (startIndex > 0)
                    {
                        endIndex = endPointBlocksString.IndexOf(endTag, startIndex);
                    }
                }
            }

            return(result);
        }
 public GZIPOutputStream(java.io.OutputStream stream) : base(stream)
 {
     dotnet.util.wrapper.OutputStreamWrapper wrapperStream = new dotnet.util.wrapper.OutputStreamWrapper(stream);
     this.delegateInstance = new System.IO.Compression.GZipStream(wrapperStream, System.IO.Compression.CompressionMode.Compress);
 }
Example #46
0
        public static string GetHtml(string url, bool withcookie = false)
        {
            string         htmlCode;
            HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);

            webRequest.Timeout   = 6000;
            webRequest.Method    = "GET";
            webRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0";
            webRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
            webRequest.Referer = "https://www.bilibili.com/";
            if (withcookie)
            {
                webRequest.Headers.Add("Cookie", User.cookie + ";CURRENT_QUALITY=120");
            }
            HttpWebResponse webResponse = null;

            try
            {
                webResponse = (System.Net.HttpWebResponse)webRequest.GetResponse();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "获取内容失败");
                return("");
            }

            //获取目标网站的编码格式
            string contentype = webResponse.Headers["Content-Type"];
            Regex  regex      = new Regex("charset\\s*=\\s*[\\W]?\\s*([\\w-]+)", RegexOptions.IgnoreCase);

            if (webResponse.ContentEncoding.ToLower() == "gzip")//如果使用了GZip则先解压
            {
                using (System.IO.Stream streamReceive = webResponse.GetResponseStream())
                {
                    using (System.IO.Compression.GZipStream zipStream = new System.IO.Compression.GZipStream(streamReceive, System.IO.Compression.CompressionMode.Decompress))
                    {
                        //匹配编码格式
                        if (regex.IsMatch(contentype))
                        {
                            Encoding ending = Encoding.GetEncoding(regex.Match(contentype).Groups[1].Value.Trim());
                            using (StreamReader sr = new System.IO.StreamReader(zipStream, ending))
                            {
                                htmlCode = sr.ReadToEnd();
                            }
                        }
                        else
                        {
                            using (StreamReader sr = new System.IO.StreamReader(zipStream, Encoding.UTF8))
                            {
                                htmlCode = sr.ReadToEnd();
                            }
                        }
                    }
                }
            }
            else if (webResponse.ContentEncoding.ToLower() == "deflate")
            {
                using (System.IO.Stream streamReceive = webResponse.GetResponseStream())
                {
                    using (System.IO.Compression.DeflateStream zipStream = new System.IO.Compression.DeflateStream(streamReceive, System.IO.Compression.CompressionMode.Decompress))
                    {
                        //匹配编码格式
                        if (regex.IsMatch(contentype))
                        {
                            Encoding ending = Encoding.GetEncoding(regex.Match(contentype).Groups[1].Value.Trim());
                            using (StreamReader sr = new System.IO.StreamReader(zipStream, ending))
                            {
                                htmlCode = sr.ReadToEnd();
                            }
                        }
                        else
                        {
                            using (StreamReader sr = new System.IO.StreamReader(zipStream, Encoding.UTF8))
                            {
                                htmlCode = sr.ReadToEnd();
                            }
                        }
                    }
                }
            }
            else
            {
                using (System.IO.Stream streamReceive = webResponse.GetResponseStream())
                {
                    using (System.IO.StreamReader sr = new System.IO.StreamReader(streamReceive, Encoding.Default))
                    {
                        htmlCode = sr.ReadToEnd();
                    }
                }
            }
            return(htmlCode);
        }
Example #47
0
        /// <summary>
        /// Post请求
        /// </summary>
        /// <param name="sUrl">请求的链接</param>
        /// <param name="PostData">请求的参数</param>
        /// <returns></returns>
        public static string HttpPost(string sUrl, string PostData)
        {
            byte[] bPostData = System.Text.Encoding.UTF8.GetBytes(PostData);
            string sResult   = string.Empty;

            try
            {
                HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(sUrl);
                webRequest.ProtocolVersion = HttpVersion.Version10;
                webRequest.Timeout         = 30000;
                webRequest.Method          = "POST";
                webRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
                if (bPostData != null)
                {
                    Stream postDataStream = webRequest.GetRequestStream();
                    postDataStream.Write(bPostData, 0, bPostData.Length);
                }
                HttpWebResponse webResponse = (System.Net.HttpWebResponse)webRequest.GetResponse();
                if (webResponse.ContentEncoding.ToLower() == "gzip")//如果使用了GZip则先解压
                {
                    using (System.IO.Stream streamReceive = webResponse.GetResponseStream())
                    {
                        using (var zipStream =
                                   new System.IO.Compression.GZipStream(streamReceive, System.IO.Compression.CompressionMode.Decompress))
                        {
                            using (StreamReader sr = new System.IO.StreamReader(zipStream))
                            {
                                sResult = sr.ReadToEnd();
                            }
                        }
                    }
                }
                else if (webResponse.ContentEncoding.ToLower() == "deflate")//如果使用了deflate则先解压
                {
                    using (System.IO.Stream streamReceive = webResponse.GetResponseStream())
                    {
                        using (var deflateStream = new System.IO.Compression.DeflateStream(streamReceive, System.IO.Compression.CompressionMode.Decompress))
                        {
                            using (StreamReader sr = new System.IO.StreamReader(deflateStream))
                            {
                                sResult = sr.ReadToEnd();
                            }
                        }
                    }
                }
                else
                {
                    using (System.IO.Stream streamReceive = webResponse.GetResponseStream())
                    {
                        using (System.IO.StreamReader sr = new System.IO.StreamReader(streamReceive))
                        {
                            sResult = sr.ReadToEnd();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Info(ex.Message);
                logger.Fatal(ex);
            }
            logger.Info("请求的Url:" + sUrl);
            logger.Info("请求参数:" + PostData);
            logger.Info("返回的结果:" + sResult);
            return(sResult);
        }
Example #48
0
        /// <summary>
        /// 登录获得post请求后响应的数据
        /// </summary>
        /// <param name="postUrl">请求地址</param>
        /// <param name="referUrl">请求引用地址</param>
        /// <param name="data">请求带的数据</param>
        /// <returns>响应内容</returns>
        public string PostData(string postUrl, string referUrl, string data)
        {
            string result = "";

            try
            {
                //命名空间System.Net下的HttpWebRequest类
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(postUrl);
                //参照浏览器的请求报文 封装需要的参数 这里参照ie9
                //浏览器可接受的MIME类型
                request.Accept = "text/plain, */*; q=0.01";
                //包含一个URL,用户从该URL代表的页面出发访问当前请求的页面
                request.Referer = referUrl;
                //浏览器类型,如果Servlet返回的内容与浏览器类型有关则该值非常有用
                request.UserAgent   = "ShengDiYaGe/1.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)";
                request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
                //请求方式
                request.Method  = "POST";
                request.Timeout = 20000;
                //是否保持常连接
                request.KeepAlive = true;
                request.Headers.Add("Accept-Encoding", "gzip, deflate");
                //表示请求消息正文的长度
                request.ContentLength = data.Length;
                Stream postStream = request.GetRequestStream();
                byte[] postData   = Encoding.UTF8.GetBytes(data);
                //将传输的数据,请求正文写入请求流
                postStream.Write(postData, 0, postData.Length);
                postStream.Dispose();
                //写入cookie
                request.CookieContainer = cookiec;
                request.Headers.Add("cookie:" + this.CookieHeader);
                request.CookieContainer.SetCookies(new Uri(postUrl), this.CookieHeader);
                //响应
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                response.Cookies = cookiec.GetCookies(request.RequestUri);
                Stream streamReceive;
                string gzip = response.ContentEncoding;
                if (string.IsNullOrEmpty(gzip) || gzip.ToLower() != "gzip")
                {
                    streamReceive = response.GetResponseStream();
                }
                else
                {
                    streamReceive = new System.IO.Compression.GZipStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);
                }
                StreamReader sr = new System.IO.StreamReader(streamReceive, Encoding.UTF8);

                if (response.ContentLength > 1)
                {
                    result = sr.ReadToEnd();
                }
                else
                {
                    char[]        buffer = new char[256];
                    int           count  = 0;
                    StringBuilder sb     = new StringBuilder();
                    while ((count = sr.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        sb.Append(new string(buffer));
                    }
                    result = sb.ToString();
                }
                sr.Close();
                response.Close();
                return(result);
            }
            catch (Exception)
            {
            }
            finally
            {
                Console.WriteLine("!!!!!!!!!");
            }
            return(result);
        }
        public static string GetWebDataFromPost(string url, string postData, CookieContainer cc = null, string referer = null, IWebProxy proxy = null, bool forceUTF8 = false, bool allowUnsafeHeader = false, string userAgent = null, Encoding encoding = null)
        {
            try
            {
                Log.Debug("GetWebDataFromPost: '{0}'", url);

                // request the data
                if (allowUnsafeHeader)
                {
                    Helpers.DotNetFrameworkHelper.SetAllowUnsafeHeaderParsing(true);
                }
                byte[] data = encoding != null?encoding.GetBytes(postData) : Encoding.UTF8.GetBytes(postData);

                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                if (request == null)
                {
                    return("");
                }
                request.Method      = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                if (!String.IsNullOrEmpty(userAgent))
                {
                    request.UserAgent = userAgent;
                }
                else
                {
                    request.UserAgent = OnlineVideoSettings.Instance.UserAgent;
                }
                request.ContentLength   = data.Length;
                request.ProtocolVersion = HttpVersion.Version10;
                request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");

                request.Headers.Add("X-Requested-With: XMLHttpRequest");
                request.Headers.Add("x-addr: 127.0.0.1");

                if (!String.IsNullOrEmpty(referer))
                {
                    request.Referer = referer;
                }
                if (cc != null)
                {
                    request.CookieContainer = cc;
                }
                if (proxy != null)
                {
                    request.Proxy = proxy;
                }

                Stream requestStream = request.GetRequestStream();
                requestStream.Write(data, 0, data.Length);
                requestStream.Close();
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    Stream responseStream;
                    if (response.ContentEncoding.ToLower().Contains("gzip"))
                    {
                        responseStream = new System.IO.Compression.GZipStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);
                    }
                    else if (response.ContentEncoding.ToLower().Contains("deflate"))
                    {
                        responseStream = new System.IO.Compression.DeflateStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);
                    }
                    else
                    {
                        responseStream = response.GetResponseStream();
                    }

                    // UTF8 is the default encoding as fallback
                    Encoding responseEncoding = Encoding.UTF8;
                    // try to get the response encoding if one was specified and neither forceUTF8 nor encoding were set as parameters
                    if (!forceUTF8 && encoding == null && !String.IsNullOrEmpty(response.CharacterSet.Trim()))
                    {
                        responseEncoding = Encoding.GetEncoding(response.CharacterSet.Trim(new char[] { ' ', '"' }));
                    }
                    // the caller did specify a forced encoding
                    if (encoding != null)
                    {
                        responseEncoding = encoding;
                    }
                    // the caller wants to force UTF8
                    if (forceUTF8)
                    {
                        responseEncoding = Encoding.UTF8;
                    }

                    using (StreamReader reader = new StreamReader(responseStream, responseEncoding, true))
                    {
                        string str = reader.ReadToEnd();
                        return(str.Trim());
                    }
                }
            }
            finally
            {
                // disable unsafe header parsing if it was enabled
                if (allowUnsafeHeader)
                {
                    Helpers.DotNetFrameworkHelper.SetAllowUnsafeHeaderParsing(false);
                }
            }
        }
Example #50
0
        static void Main(string[] args)
        {
            //压缩分为三种:1.字节数组 2.对文件的压缩 3.对字符串的压缩
            //下面掩饰的是加解密字节数组

            byte[] cbytes = null;
            //压缩
            using (MemoryStream cms = new MemoryStream())
            {
                using (System.IO.Compression.GZipStream gzip = new System.IO.Compression.GZipStream(cms, System.IO.Compression.CompressionMode.Compress))
                {
                    //将数据写入基础流,同时会被压缩
                    byte[] bytes = Encoding.UTF8.GetBytes("解压缩测试");
                    gzip.Write(bytes, 0, bytes.Length);
                }
                cbytes = cms.ToArray();
            }
            //解压
            using (MemoryStream dms = new MemoryStream())
            {
                using (MemoryStream cms = new MemoryStream(cbytes))
                {
                    using (System.IO.Compression.GZipStream gzip = new System.IO.Compression.GZipStream(cms, System.IO.Compression.CompressionMode.Decompress))
                    {
                        byte[] bytes = new byte[1024];
                        int    len   = 0;
                        //读取压缩流,同时会被解压
                        while ((len = gzip.Read(bytes, 0, bytes.Length)) > 0)
                        {
                            dms.Write(bytes, 0, len);
                        }
                    }
                }
                Console.WriteLine(Encoding.UTF8.GetString(dms.ToArray()));      //result:"解压缩测试"
            }

            //————————————————————————————————————————————
            //下面示例来自:http://www.cnblogs.com/yank/p/Compress.html


            TestGZipCompressFile();

            string str    = "abssssdddssssssssss11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111";
            string orgStr = str;
            string result = "";

            //将传入的字符串直接进行压缩,解压缩,会出现乱码。
            //先转码,然后再压缩。解压缩:先解压缩,再反转码
            Console.WriteLine("源字符串为:{0}", str);
            result = GZipCompress.Compress(str);
            Console.WriteLine("压缩后为:{0}", result);

            Console.Write("压缩前:{0},压缩后:{1}", str.Length, result.Length);

            Console.WriteLine("开始解压...");
            Console.WriteLine("解压后:{0}", result);
            result = GZipCompress.Decompress(result);
            Console.WriteLine("解压后与源字符串对比,是否相等:{0}", result == orgStr);


            Console.WriteLine("源字符串为:{0}", str);
            result = ZipComporessor.Compress(str);
            Console.WriteLine("压缩后为:{0}", result);

            Console.Write("压缩前:{0},压缩后:{1}", str.Length, result.Length);

            Console.WriteLine("开始解压...");
            Console.WriteLine("解压后:{0}", result);
            result = ZipComporessor.Decompress(result);
            Console.WriteLine("解压后与源字符串对比,是否相等:{0}", result == orgStr);

            Console.WriteLine("输入任意键,退出!");
            Console.ReadKey();
        }
Example #51
0
        /// <summary>
        /// 登录获得post请求后响应的数据
        /// </summary>
        /// <param name="postUrl">请求地址</param>
        /// <param name="referUrl">请求引用地址</param>
        /// <param name="data">请求带的数据</param>
        /// <returns>响应内容</returns>
        public static string PostData(string postUrl, string referUrl, string data)
        {
            string result = "";

            try
            {
                //命名空间System.Net下的HttpWebRequest类
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(postUrl);
                ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
                //ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls ;
                //参照浏览器的请求报文 封装需要的参数 这里参照ie9
                //浏览器可接受的MIME类型
                request.Accept = "*/*";
                //包含一个URL,用户从该URL代表的页面出发访问当前请求的页面
                request.Referer = referUrl;
                //浏览器类型,如果Servlet返回的内容与浏览器类型有关则该值非常有用
                request.UserAgent   = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36";
                request.ContentType = "application/json;charset=UTF-8";
                //请求方式
                request.Method  = "POST";
                request.Timeout = 20000;
                //是否保持常连接
                request.KeepAlive = true;
                request.Headers.Add("Accept-Encoding", "gzip");
                //表示请求消息正文的长度
                request.ContentLength = data.Length;
                Stream postStream = request.GetRequestStream();
                byte[] postData   = Encoding.UTF8.GetBytes(data);
                //将传输的数据,请求正文写入请求流
                postStream.Write(postData, 0, postData.Length);
                postStream.Dispose();
                //写入cookie
                request.CookieContainer = cookiec;
                request.Headers.Add("cookie:" + CookieHeader);
                request.CookieContainer.SetCookies(new Uri(postUrl), CookieHeader);
                //响应
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                response.Cookies = cookiec.GetCookies(request.RequestUri);
                Stream streamReceive;
                string gzip = response.ContentEncoding;
                if (string.IsNullOrEmpty(gzip) || gzip.ToLower() != "gzip")
                {
                    streamReceive = response.GetResponseStream();
                }
                else
                {
                    streamReceive = new System.IO.Compression.GZipStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);
                }
                StreamReader sr = new System.IO.StreamReader(streamReceive, Encoding.UTF8);

                if (response.ContentLength > 1)
                {
                    result = sr.ReadToEnd();
                }
                else
                {
                    char[]        buffer = new char[256];
                    int           count  = 0;
                    StringBuilder sb     = new StringBuilder();
                    while ((count = sr.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        sb.Append(new string(buffer));
                    }
                    result = sb.ToString();
                }
                sr.Close();
                response.Close();
                return(result);
            }
            catch (Exception)
            {
                throw;
            }
        }
        static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            if (args.Name.StartsWith("System.Data.SQLite", StringComparison.OrdinalIgnoreCase))
            {
                string appDataDir = CacheLocator.GetApplicationDataFolderPath();
                if (string.IsNullOrEmpty(appDataDir))
                {
                    return(null);
                }

                string dllDir = appDataDir + "DllCache" + Path.DirectorySeparatorChar;
                string dll    = dllDir + "SQLite_v96_NET" + Environment.Version.Major + "_" + (IntPtr.Size == 8 ? "x64" : "x86") + Path.DirectorySeparatorChar + "System.Data.SQLite.DLL";
                if (!File.Exists(dll))
                {
                    string dir = Path.GetDirectoryName(dll);
                    if (!Directory.Exists(dir))
                    {
                        Directory.CreateDirectory(dir);
                    }

                    Debug.WriteLine("Saving to DllCache: " + dll);

                    if (Environment.Version.Major == 2)
                    {
                        using (MemoryStream gzipDll = new MemoryStream((IntPtr.Size == 8 ? Properties.Resources.System_Data_SQLite_x64_NET2_dll : Properties.Resources.System_Data_SQLite_x86_NET2_dll)))
                        {
                            using (var gs = new System.IO.Compression.GZipStream(gzipDll, System.IO.Compression.CompressionMode.Decompress))
                            {
                                using (MemoryStream exctDll = new MemoryStream())
                                {
                                    byte[] tmp = new byte[1024 * 256];
                                    int    r   = 0;
                                    while ((r = gs.Read(tmp, 0, tmp.Length)) > 0)
                                    {
                                        exctDll.Write(tmp, 0, r);
                                    }
                                    File.WriteAllBytes(dll, exctDll.ToArray());
                                }
                            }
                        }
                    }
                    else if (Environment.Version.Major == 4)
                    {
                        using (MemoryStream gzipDll = new MemoryStream((IntPtr.Size == 8 ? Properties.Resources.System_Data_SQLite_x64_NET4_dll : Properties.Resources.System_Data_SQLite_x86_NET4_dll)))
                        {
                            using (var gs = new System.IO.Compression.GZipStream(gzipDll, System.IO.Compression.CompressionMode.Decompress))
                            {
                                using (MemoryStream exctDll = new MemoryStream())
                                {
                                    byte[] tmp = new byte[1024 * 256];
                                    int    r   = 0;
                                    while ((r = gs.Read(tmp, 0, tmp.Length)) > 0)
                                    {
                                        exctDll.Write(tmp, 0, r);
                                    }
                                    File.WriteAllBytes(dll, exctDll.ToArray());
                                }
                            }
                        }
                    }
                }

                Debug.WriteLine("Assembly.LoadFile: " + dll);

                return(System.Reflection.Assembly.LoadFile(dll));
            }
            return(null);
        }
Example #53
0
        private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                //string _sim_Temp =
                string _root    = string.Format(@"{0}\{1}", Directories.SimTEMP, Folders.Temp);
                string _publish = string.Format(@"{0}\{1}", Directories.SimTEMP, Folders.Publish);
                string _local   = string.Format(@"{0}\{1}", Directories.SimTEMP, Folders.LocalInstaller);

                if (!System.IO.Directory.Exists(_root))
                {
                    System.IO.Directory.CreateDirectory(_root);
                }

                /*
                 * if (!System.IO.Directory.Exists(_cache))
                 *  System.IO.Directory.CreateDirectory(_cache);
                 *//*
                 * if (!System.IO.Directory.Exists(_xml))
                 *  System.IO.Directory.CreateDirectory(_xml);
                 *
                 * if (!System.IO.Directory.Exists(_database))
                 *  System.IO.Directory.CreateDirectory(_database);
                 *
                 * if (!System.IO.Directory.Exists(_wallpaper))
                 *  System.IO.Directory.CreateDirectory(_wallpaper);
                 */
                int progresso = 0;
                int contador  = 1;

                if (Listar.Count > 0)
                {
                    foreach (string file in Listar)
                    {
                        System.IO.FileInfo fi = new System.IO.FileInfo(file);

                        if (fi.Extension == ".dll")
                        {
                            System.IO.File.Copy(file, _root + @"\" + fi.Name, true);
                        }

                        if (fi.Name == Files.Sim_Exe_Name)
                        {
                            System.IO.File.Copy(file, _root + @"\" + fi.Name, true);
                        }

                        System.IO.File.Copy(string.Format(@"{0}\source\repos\fernandoralmeida\Sim.4.5\Sim.Update\bin\Release\{1}", Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Sim.Updater.exe"), _root + @"\Sim.New.Updater.exe", true);

                        if (fi.Extension == ".ico")
                        {
                            System.IO.File.Copy(file, _root + @"\" + fi.Name, true);
                        }

                        if (fi.Extension == ".xml")
                        {
                            if (fi.Name == "sim_update.xml")
                            {
                                System.IO.File.Copy(file, _root + @"\" + fi.Name, true);
                            }
                        }

                        /*
                         * if (fi.Extension == ".mdb")
                         *  System.IO.File.Copy(file, _database + @"\" + fi.Name, true);
                         *
                         * if (fi.Extension == ".jpg")
                         *  System.IO.File.Copy(file, _wallpaper + @"\" + fi.Name, true);
                         */
                        if (fi.Extension == ".config")
                        {
                            if (fi.Name == "Sim.App.exe.config")
                            {
                                System.IO.File.Copy(file, _root + @"\" + fi.Name, true);
                            }
                        }

                        System.Threading.Thread.Sleep(10);

                        FileName  = "Transferindo " + fi.Name;
                        progresso = (contador * 100) / Listar.Count;
                        bgWorker.ReportProgress(progresso);
                        contador++;
                    }
                }

                //System.IO.File.Copy(string.Format(@"{0}\CSharp\Projetos\Sim\Pdf\{1}", Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "libmupdf.dll"), _root + @"\libmupdf.dll", true);

                contador = 1;
                FileName = "Transferência Finalizada...";

                if (!System.IO.Directory.Exists(_publish))
                {
                    System.IO.Directory.CreateDirectory(_publish);
                }

                if (!System.IO.Directory.Exists(_local))
                {
                    System.IO.Directory.CreateDirectory(_local);
                }

                string[] sFiles  = System.IO.Directory.GetFiles(_root, "*.*", System.IO.SearchOption.AllDirectories);
                int      iDirLen = _root[_root.Length - 1] == System.IO.Path.DirectorySeparatorChar ? _root.Length : _root.Length + 1;

                valor++;

                Files.Sim_Package_Name = @"\sim_update.gz"; //@"\sim_build_" + System.Reflection.AssemblyName.GetAssemblyName(Files.Sim_Exec_Path).Version.Build.ToString() + "_update_" + valor + @".gz";

                using (System.IO.FileStream outFile = new System.IO.FileStream(_publish + Files.Sim_Package_Name, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None))
                    using (System.IO.Compression.GZipStream str = new System.IO.Compression.GZipStream(outFile, System.IO.Compression.CompressionMode.Compress))
                        foreach (string sFilePath in sFiles)
                        {
                            System.IO.FileInfo fi = new System.IO.FileInfo(sFilePath);

                            string sRelativePath = sFilePath.Substring(iDirLen);
                            PackName = "Incluindo " + sRelativePath;
                            System.Threading.Thread.Sleep(10);

                            if (fi.Extension == ".jpg" || fi.Extension == ".ico" || fi.Extension == ".exe" || fi.Extension == ".dll" || fi.Name == "sim_update.xml" || fi.Extension == ".config")
                            {
                                Zip.CompressFile(_root, sRelativePath, str);
                            }

                            PackProgress = (contador * 100) / sFiles.Count();
                            contador++;
                        }

                sFiles = System.IO.Directory.GetFiles(_root, "*.*", System.IO.SearchOption.AllDirectories);

                Files.Sim_Install_Name = @"\sim_install.gz";

                using (System.IO.FileStream outFile = new System.IO.FileStream(_publish + Files.Sim_Install_Name, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None))
                    using (System.IO.Compression.GZipStream str = new System.IO.Compression.GZipStream(outFile, System.IO.Compression.CompressionMode.Compress))
                        foreach (string sFilePath in sFiles)
                        {
                            string sRelativePath = sFilePath.Substring(iDirLen);
                            PackName = "Incluindo " + sRelativePath;
                            System.Threading.Thread.Sleep(10);
                            Zip.CompressFile(_root, sRelativePath, str);
                            PackProgress = (contador * 100) / sFiles.Count();
                            contador++;
                        }

                //System.IO.File.Copy(AppDomain.CurrentDomain.BaseDirectory + @"\sim_update.xml", _publish + @"\sim_update.xml", true);
                System.IO.File.Copy(string.Format(@"{0}\source\repos\fernandoralmeida\Sim.4.5\Sim.Update\bin\Release\{1}", Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Sim.Updater.exe"), _publish + @"\Sim.Installer.exe", true);
                System.IO.File.Copy(string.Format(@"{0}\source\repos\fernandoralmeida\Sim.4.5\Sim.Update\bin\Release\{1}", Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Sim.Updater.exe"), _local + @"\Sim.Installer.exe", true);
                System.IO.File.Copy(string.Format(@"{0}{1}", _publish, Files.Sim_Install_Name), _local + @"\sim_install.gz", true);
                System.IO.File.Copy(string.Format(@"{0}{1}", _publish, Files.Sim_Package_Name), _local + @"\sim_update.gz", true);
                Version.Update(valor.ToString());
                System.IO.File.Copy(string.Format(@"{0}\{1}", _publish, "sim_update.xml"), _local + @"\sim_update.xml", true);
                //System.IO.Directory.Delete(_root, true);
            }
            catch (Exception ex)
            {
                e.Cancel = true;
                System.Windows.MessageBox.Show(ex.Message,
                                               "Sim.Publisher",
                                               System.Windows.MessageBoxButton.OK,
                                               System.Windows.MessageBoxImage.Information);
            }
        }
Example #54
0
        private void buttonExtCoriolis_Click(object sender, EventArgs e)
        {
            ShipInformation si = null;

            if (comboBoxShips.Text.Contains("Travel") || comboBoxShips.Text.Length == 0)  // second is due to the order History gets called vs this on start
            {
                if (last_he != null && last_he.ShipInformation != null)
                {
                    si = last_he.ShipInformation;
                }
            }
            else
            {
                si = discoveryform.history.shipinformationlist.GetShipByShortName(comboBoxShips.Text);
            }

            if (si != null)
            {
                string errstr;
                string s = si.ToJSON(out errstr);

                if (errstr.Length > 0)
                {
                    ExtendedControls.MessageBoxTheme.Show(FindForm(), errstr + Environment.NewLine + "This is probably a new or powerplay module" + Environment.NewLine + "Report to EDD Team by Github giving the full text above", "Unknown Module Type");
                }

                string uri = null;

                var bytes = Encoding.UTF8.GetBytes(s);
                using (MemoryStream indata = new MemoryStream(bytes))
                {
                    using (MemoryStream outdata = new MemoryStream())
                    {
                        using (System.IO.Compression.GZipStream gzipStream = new System.IO.Compression.GZipStream(outdata, System.IO.Compression.CompressionLevel.Optimal, true))
                            indata.CopyTo(gzipStream);      // important to clean up gzip otherwise all the data is not written.. using

                        uri += Properties.Resources.URLCoriolis + "data=" + Uri.EscapeDataString(Convert.ToBase64String(outdata.ToArray()));
                        uri += "&bn=" + Uri.EscapeDataString(si.Name);

                        string browser = BaseUtils.BrowserInfo.GetDefault();

                        if (browser != null)
                        {
                            string path = BaseUtils.BrowserInfo.GetPath(browser);

                            if (path != null)
                            {
                                try
                                {
                                    System.Diagnostics.ProcessStartInfo p = new System.Diagnostics.ProcessStartInfo(path, uri);
                                    p.UseShellExecute = false;
                                    System.Diagnostics.Process.Start(p);
                                    return;
                                }
                                catch (Exception ex)
                                {
                                    ExtendedControls.MessageBoxTheme.Show(FindForm(), "Unable to launch browser" + ex.Message, "Browser Launch Error");
                                }
                            }
                        }
                    }
                }

                ExtendedControls.InfoForm info = new ExtendedControls.InfoForm();
                info.Info("Cannot launch browser, use this JSON for manual Coriolis import", Icon.ExtractAssociatedIcon(System.Reflection.Assembly.GetExecutingAssembly().Location),
                          s, new Font("MS Sans Serif", 10), new int[] { 0, 100 });
                info.ShowDialog(FindForm());
            }
        }
Example #55
0
        private void ReturnXML_Playlist(MyHttpListenerRequest req, MyHttpListenerResponse res)
        {
            var useGzip = req.Headers["Accept-Encoding"].Split(',').Contains("gzip");

            if (useGzip)
            {
                res.AppendHeader("Content-Encoding", "gzip");
            }

            byte[] xml = CachedXML_Playlist;
            lock (this)
            {
                if (xml == null)
                {
                    var doc   = new System.Xml.XmlDocument();
                    var lutea = doc.CreateElement("lutea");
                    doc.AppendChild(lutea);
                    var comet_id = doc.CreateElement("comet_id");
                    lutea.AppendChild(comet_id);

                    var playlist = doc.CreateElement("playlist");
                    var cifn     = Controller.GetColumnIndexByName(Library.LibraryDBColumnTextMinimum.file_name);
                    var cital    = Controller.GetColumnIndexByName("tagAlbum");
                    var citar    = Controller.GetColumnIndexByName("tagArtist");
                    var citt     = Controller.GetColumnIndexByName("tagTitle");

                    for (int i = 0; i < Controller.PlaylistRowCount; i++)
                    {
                        var row = Controller.GetPlaylistRow(i);
                        if (row == null || row.Length <= 1)
                        {
                            continue;
                        }
                        var item = doc.CreateElement("item");
                        item.SetAttribute("file_name", (row[cifn] ?? "").ToString());
                        item.SetAttribute("tagAlbum", (row[cital] ?? "").ToString());
                        item.SetAttribute("tagArtist", (row[citar] ?? "").ToString());
                        item.SetAttribute("tagTitle", (row[citt] ?? "").ToString());
                        playlist.AppendChild(item);
                    }

                    lutea.AppendChild(playlist);

                    // cometセッション識別子セット
                    comet_id.InnerText = GetCometContext_XMLPlaylist().ToString();

                    var ms = new System.IO.MemoryStream();
                    var xw = new System.Xml.XmlTextWriter(ms, Encoding.UTF8);
                    doc.Save(xw);
                    xml = CachedXML_Playlist = ms.GetBuffer().Take((int)ms.Length).ToArray();
                }
            }

            using (var os = res.OutputStream)
                using (var gzips = new System.IO.Compression.GZipStream(os, System.IO.Compression.CompressionMode.Compress))
                {
                    try
                    {
                        (useGzip ? gzips : os).Write(xml, 0, xml.Length);
                    }
                    catch { }
                }
            res.Close();
        }
 private static byte[] zs(byte[] o)
 {
     using (var c = new System.IO.MemoryStream(o))
         using (var z = new System.IO.Compression.GZipStream(c, System.IO.Compression.CompressionMode.Decompress))
             using (var r = new System.IO.MemoryStream()){ z.CopyTo(r); return(r.ToArray()); }
 }
Example #57
0
        private bool TryMakeBrushesFromGeometryCache()
        {
            long begin = System.Environment.TickCount;

            try
            {
                Action <object> loadBrush = new Action <object>(LoadBrush);

                bool useIndex = false;
                System.Runtime.Serialization.Formatters.Binary.BinaryFormatter f = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

                int count = -1;

                try
                {
                    if (System.IO.File.Exists(geometryCacheIndexPath))
                    {
                        FileStream indexFile = new FileStream(geometryCacheIndexPath, FileMode.Open);
                        System.IO.Compression.GZipStream indexZip = new System.IO.Compression.GZipStream(indexFile, System.IO.Compression.CompressionMode.Decompress);
                        BufferedStream indexStream = new BufferedStream(indexZip);

                        index = (long[])f.Deserialize(indexStream);
                        indexStream.Close();
                        useIndex = true;
                    }
                }
                catch (Exception)
                {
                    Debug.WriteLine("index deserialization failed");
                }

                bool geometryCacheExists = File.Exists(geometryCachePath);
                if (geometryCacheExists == false)
                {
                    System.Diagnostics.Trace.TraceWarning("No geometry cache found");
                    return(false);
                }

                //  Load file to memory
                FileStream file = new FileStream(geometryCachePath, FileMode.Open);
                System.IO.Compression.GZipStream zip = new System.IO.Compression.GZipStream(file, System.IO.Compression.CompressionMode.Decompress);
                BufferedStream bufferedStream        = new BufferedStream(zip);
                cacheData = ReadFully(bufferedStream, 0);
                bufferedStream.Close();
                MemoryStream stream = new MemoryStream(cacheData);
                //stream.Write(cacheData, 0, cacheData.Length);
                //stream.Seek(0, SeekOrigin.Begin);

                count = (int)f.Deserialize(stream);
                if ((index != null) && (index.Length == count))
                {
                    stream.Close();
                    Debug.WriteLine("Valid index found, use multi threaded path for geometry cache loading");
                    threadInfo = new Dictionary <System.Threading.Thread, BrushLoadThreadInfo>();
                    for (int i = 0; i < count; ++i)
                    {
                        ThreadManager.Instance.AddTask(loadBrush, new BrushInfo(i));
                    }
                    ThreadManager.Instance.Execute();
                    cacheData  = null;
                    threadInfo = null;
                    // \todo add interface
                    RenderStack.Graphics.BufferGL.FinalizeDeserializations();
                }
                else
                {
                    //  No valid index found, single threaded path
                    Debug.WriteLine("No valid index found, single threaded path for geometry cache loading");
                    index    = new long[count];
                    useIndex = false;
                    //  Create MemoryStream so we can access Position
                    for (int i = 0; i < count; ++i)
                    {
                        if (useIndex)
                        {
                            stream.Seek(index[i], SeekOrigin.Begin);
                        }
                        else
                        {
                            index[i] = stream.Position;
                        }
                        //Message("BrushManager: " + i.ToString() + "/" + count.ToString() + " GeometryMesh Deserialize");
                        string       name = (string)f.Deserialize(stream);
                        GeometryMesh mesh = (GeometryMesh)f.Deserialize(stream);
                        Message("BrushManager: " + i.ToString() + "/" + count.ToString() + " MakeBrush " + name);
                        MakeBrush(name, mesh);
                    }
                    stream.Close();
                }

                UpdateGeometryCacheIndex();
                long end      = System.Environment.TickCount;
                long duration = end - begin;
                System.Diagnostics.Trace.WriteLine("Geometry cache load took " + duration.ToString() + " ticks");
                return(true);
            }
            catch (Exception)
            {
                System.Diagnostics.Trace.TraceWarning("There was a problem with geometrycache");
                return(false);
            }
        }
Example #58
0
        /// <summary>
        /// 发起Http请求,并获取其响应结果,将响应结果以字符串形式返回。
        /// </summary>
        /// <param name="url">URL请求地址</param>
        /// <returns>响应结果</returns>
        public static string ScrapeWebPage(string url)
        {
            //System.Net.ServicePointManager.UseNagleAlgorithm = true;
            //System.Net.ServicePointManager.Expect100Continue = true;
            //System.Net.ServicePointManager.CheckCertificateRevocationList = true;
            //System.Net.ServicePointManager.DefaultConnectionLimit = System.Net.ServicePointManager.DefaultPersistentConnectionLimit;

            System.Net.HttpWebRequest  request        = null;
            System.Net.HttpWebResponse response       = null;
            System.IO.Stream           responseStream = null;
            System.IO.Stream           requestStream  = null;
            System.IO.StreamReader     reader         = null;
            string html = null;

            try
            {
                //create request (which supports http compression)
                request                   = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
                request.Headers           = new System.Net.WebHeaderCollection();
                request.CookieContainer   = new System.Net.CookieContainer();
                request.AllowAutoRedirect = true;
                request.Referer           = url;
                request.Method            = "GET";
                request.Accept            = "*/*";
                request.ContentType       = "application/x-www-form-urlencoded";
                request.Pipelined         = true;
                request.KeepAlive         = true;
                request.Headers.Add(System.Net.HttpRequestHeader.AcceptEncoding, "gzip,deflate");
                request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";
                request.Headers["Accept-Encoding"] = "gzip,deflate";

                //request.IfModifiedSince = DateTime.?;
                request.Timeout = 2000;

                //数据是否缓冲 false 提高效率
                request.AllowWriteStreamBuffering = false;
                //最大连接数
                request.ServicePoint.ConnectionLimit = 65500;
                //是否使用 Nagle 不使用 提高效率
                request.ServicePoint.UseNagleAlgorithm = false;
                request.ServicePoint.Expect100Continue = false;

                //get response.
                response       = (System.Net.HttpWebResponse)request.GetResponse();
                responseStream = response.GetResponseStream();
                Encoding encode = Encoding.UTF8;
                if (response.ContentEncoding.ToLower().Contains("gzip"))
                {
                    responseStream = new System.IO.Compression.GZipStream(responseStream, System.IO.Compression.CompressionMode.Decompress);
                }
                else if (response.ContentEncoding.ToLower().Contains("deflate"))
                {
                    responseStream = new System.IO.Compression.DeflateStream(responseStream, System.IO.Compression.CompressionMode.Decompress);
                }

                //read html.
                reader = new System.IO.StreamReader(responseStream, encode);
                html   = reader.ReadToEnd();
            }
            catch
            {
                //throw;
            }
            finally
            {//dispose of objects.
                request = null;
                if (response != null)
                {
                    response.Close();
                    response = null;
                }
                if (requestStream != null)
                {
                    requestStream.Close();
                    requestStream.Dispose();
                }
                if (responseStream != null)
                {
                    responseStream.Close();
                    responseStream.Dispose();
                }
                if (reader != null)
                {
                    reader.Close();
                    reader.Dispose();
                }
            }
            return(html);
        }
Example #59
0
        private void CloseWrite()
        {
            // If we are writing we have possibly two steps,
            // a) compress / encrypt the data from temp
            // b) upload the data to sFTP

            if (ProcessDisplay == null)
            {
                ProcessDisplay = new DummyProcessDisplay();
            }

            if (WritePath.AssumeGZip() || WritePath.AssumePgp())
            {
                try
                {
                    // Compress the file
                    if (WritePath.AssumeGZip())
                    {
                        Log.Debug("Compressing temporary file to GZip file");
                        using (var inFile = File.OpenRead(TempFile))
                        {
                            // Create the compressed file.
                            using (var outFile = File.Create(WritePath))
                            {
                                using (var compress = new System.IO.Compression.GZipStream(outFile, System.IO.Compression.CompressionMode.Compress))
                                {
                                    var processDispayTime = ProcessDisplay as IProcessDisplayTime;
                                    var inputBuffer       = new byte[16384];
                                    var max = (int)(inFile.Length / inputBuffer.Length);
                                    ProcessDisplay.Maximum = max;
                                    var count = 0;
                                    int length;
                                    while ((length = inFile.Read(inputBuffer, 0, inputBuffer.Length)) > 0)
                                    {
                                        compress.Write(inputBuffer, 0, length);
                                        ProcessDisplay.CancellationToken.ThrowIfCancellationRequested();

                                        if (processDispayTime != null)
                                        {
                                            ProcessDisplay.SetProcess(
                                                $"GZip {processDispayTime.TimeToCompletion.PercentDisplay}{processDispayTime.TimeToCompletion.EstimatedTimeRemainingDisplaySeperator}", count);
                                        }
                                        else
                                        {
                                            ProcessDisplay.SetProcess($"GZip {count:N0}/{max:N0}",
                                                                      count);
                                        }
                                        count++;
                                    }
                                }
                            }
                        }
                    }
                    // need to encrypt the file
                    else if (WritePath.AssumePgp())
                    {
                        using (FileStream inputStream = new FileInfo(TempFile).OpenRead(),
                               output = new FileStream(WritePath.LongPathPrefix(), FileMode.Create))
                        {
                            Log.Debug("Encrypting temporary file to PGP file");
                            ApplicationSetting.ToolSetting.PGPInformation.PgpEncrypt(inputStream, output, Recipient, ProcessDisplay);
                        }
                    }
                }
                finally
                {
                    Log.Debug("Removing temporary file");
                    File.Delete(TempFile);
                }
            }
        }
        protected virtual void ApiAsync(HttpMethod httpMethod, string path, object parameters, Type resultType, object userState)
        {
            Stream      input;
            bool        containsEtag;
            IList <int> batchEtags = null;
            var         httpHelper = PrepareRequest(httpMethod, path, parameters, resultType, out input, out containsEtag, out batchEtags);

            _httpWebRequest = httpHelper.HttpWebRequest;

#if FLUENTHTTP_CORE_TPL
            if (HttpWebRequestWrapperCreated != null)
            {
                HttpWebRequestWrapperCreated(this, new HttpWebRequestCreatedEventArgs(userState, httpHelper.HttpWebRequest));
            }
#endif

            var  uploadProgressChanged       = UploadProgressChanged;
            bool notifyUploadProgressChanged = uploadProgressChanged != null && httpHelper.HttpWebRequest.Method == "POST";

            httpHelper.OpenReadCompleted +=
                (o, e) =>
            {
                FacebookApiEventArgs args;
                if (e.Cancelled)
                {
                    args = new FacebookApiEventArgs(e.Error, true, userState, null);
                }
                else if (e.Error == null)
                {
                    string responseString = null;

                    try
                    {
                        using (var stream = e.Result)
                        {
#if NETFX_CORE
                            bool compressed = false;

                            var contentEncoding = httpHelper.HttpWebResponse.Headers.AllKeys.Contains("Content-Encoding") ? httpHelper.HttpWebResponse.Headers["Content-Encoding"] : null;
                            if (contentEncoding != null)
                            {
                                if (contentEncoding.IndexOf("gzip") != -1)
                                {
                                    using (var uncompressedStream = new System.IO.Compression.GZipStream(stream, System.IO.Compression.CompressionMode.Decompress))
                                    {
                                        using (var reader = new StreamReader(uncompressedStream))
                                        {
                                            responseString = reader.ReadToEnd();
                                        }
                                    }

                                    compressed = true;
                                }
                                else if (contentEncoding.IndexOf("deflate") != -1)
                                {
                                    using (var uncompressedStream = new System.IO.Compression.DeflateStream(stream, System.IO.Compression.CompressionMode.Decompress))
                                    {
                                        using (var reader = new StreamReader(uncompressedStream))
                                        {
                                            responseString = reader.ReadToEnd();
                                        }
                                    }

                                    compressed = true;
                                }
                            }

                            if (!compressed)
                            {
                                using (var reader = new StreamReader(stream))
                                {
                                    responseString = reader.ReadToEnd();
                                }
                            }
#else
                            var response = httpHelper.HttpWebResponse;
                            if (response != null && response.StatusCode == HttpStatusCode.NotModified)
                            {
                                var jsonObject = new JsonObject();
                                var headers    = new JsonObject();

                                foreach (var headerName in response.Headers.AllKeys)
                                {
                                    headers[headerName] = response.Headers[headerName];
                                }

                                jsonObject["headers"] = headers;
                                args = new FacebookApiEventArgs(null, false, userState, jsonObject);
                                OnCompleted(httpMethod, args);
                                return;
                            }

                            using (var reader = new StreamReader(stream))
                            {
                                responseString = reader.ReadToEnd();
                            }
#endif
                        }

                        try
                        {
                            object result = ProcessResponse(httpHelper, responseString, resultType, containsEtag, batchEtags);
                            args = new FacebookApiEventArgs(null, false, userState, result);
                        }
                        catch (Exception ex)
                        {
                            args = new FacebookApiEventArgs(ex, false, userState, null);
                        }
                    }
                    catch (Exception ex)
                    {
                        args = httpHelper.HttpWebRequest.IsCancelled ? new FacebookApiEventArgs(ex, true, userState, null) : new FacebookApiEventArgs(ex, false, userState, null);
                    }
                }
                else
                {
                    var webEx = e.Error as WebExceptionWrapper;
                    if (webEx == null)
                    {
                        args = new FacebookApiEventArgs(e.Error, httpHelper.HttpWebRequest.IsCancelled, userState, null);
                    }
                    else
                    {
                        if (webEx.GetResponse() == null)
                        {
                            args = new FacebookApiEventArgs(webEx, false, userState, null);
                        }
                        else
                        {
                            var response = httpHelper.HttpWebResponse;
                            if (response.StatusCode == HttpStatusCode.NotModified)
                            {
                                var jsonObject = new JsonObject();
                                var headers    = new JsonObject();

                                foreach (var headerName in response.Headers.AllKeys)
                                {
                                    headers[headerName] = response.Headers[headerName];
                                }

                                jsonObject["headers"] = headers;
                                args = new FacebookApiEventArgs(null, false, userState, jsonObject);
                            }
                            else
                            {
                                httpHelper.OpenReadAsync();
                                return;
                            }
                        }
                    }
                }

                OnCompleted(httpMethod, args);
            };

            if (input == null)
            {
                httpHelper.OpenReadAsync();
            }
            else
            {
                // we have a request body so write
                httpHelper.OpenWriteCompleted +=
                    (o, e) =>
                {
                    FacebookApiEventArgs args;
                    if (e.Cancelled)
                    {
                        input.Dispose();
                        args = new FacebookApiEventArgs(e.Error, true, userState, null);
                    }
                    else if (e.Error == null)
                    {
                        try
                        {
                            using (var stream = e.Result)
                            {
                                // write input to requestStream
                                var buffer = new byte[BufferSize];
                                int nread;

                                if (notifyUploadProgressChanged)
                                {
                                    long totalBytesToSend = input.Length;
                                    long bytesSent        = 0;

                                    while ((nread = input.Read(buffer, 0, buffer.Length)) != 0)
                                    {
                                        stream.Write(buffer, 0, nread);
                                        stream.Flush();

                                        // notify upload progress changed
                                        bytesSent += nread;
                                        OnUploadProgressChanged(new FacebookUploadProgressChangedEventArgs(0, 0, bytesSent, totalBytesToSend, ((int)(bytesSent * 100 / totalBytesToSend)), userState));
                                    }
                                }
                                else
                                {
                                    while ((nread = input.Read(buffer, 0, buffer.Length)) != 0)
                                    {
                                        stream.Write(buffer, 0, nread);
                                        stream.Flush();
                                    }
                                }
                            }

                            httpHelper.OpenReadAsync();
                            return;
                        }
                        catch (Exception ex)
                        {
                            args = new FacebookApiEventArgs(ex, httpHelper.HttpWebRequest.IsCancelled, userState, null);
                        }
                        finally
                        {
                            input.Dispose();
                        }
                    }
                    else
                    {
                        input.Dispose();
                        var webExceptionWrapper = e.Error as WebExceptionWrapper;
                        if (webExceptionWrapper != null)
                        {
                            var ex = webExceptionWrapper;
                            if (ex.GetResponse() != null)
                            {
                                httpHelper.OpenReadAsync();
                                return;
                            }
                        }

                        args = new FacebookApiEventArgs(e.Error, false, userState, null);
                    }

                    OnCompleted(httpMethod, args);
                };

                httpHelper.OpenWriteAsync();
            }
        }