Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Thursday, June 25, 2015

C# byte [ ] Array Compress and Decompress with GZipStream

A small utility class for compressing and decompressing byte [ ] array in C#

using System.IO;
using System.IO.Compression;

public static class Compression
{
    public static byte[] Compress(byte[] data)
    {
        using (var ms = new MemoryStream())
        {
            using (var gzip = new GZipStream(ms, CompressionLevel.Optimal))
            {
                gzip.Write(data, 0, data.Length);
            }
            data = ms.ToArray();
        }
        return data;
    }

    public static byte[] Decompress(byte[] data)
    {
        // the trick is to read the last 4 bytes to get the length
        // gzip appends this to the array when compressing
        var lengthBuffer = new byte[4];
        Array.Copy(data, data.Length - 4, lengthBuffer, 0, 4);
        int uncompressedSize = BitConverter.ToInt32(lengthBuffer, 0);
        var buffer = new byte[uncompressedSize];
        using (var ms = new MemoryStream(data))
        {
            using (var gzip = new GZipStream(ms, CompressionMode.Decompress))
            {
                gzip.Read(buffer, 0, uncompressedSize);
            }
        }
        return buffer;
    }
}

Tuesday, March 4, 2014

How to send HTTPS Post Request with C#

  • Create new C# website application.
  • Open the default code behind file i.e. Default.aspx.cs
  • add the following two namespace
    using System.Security.Cryptography.X509Certificates;
    using System.Net;
For the https request we have to create a CertificatePolicy  subclass which is

public class MyPolicy : ICertificatePolicy
{
    public bool CheckValidationResult(ServicePoint srvPoint,
      X509Certificate certificate, WebRequest request,
      int certificateProblem)
    {
        //Return True to force the certificate to be accepted.
        return true;
    }
}

Now go to the page load event and paste the following code.
protected void Page_Load(object sender, EventArgs e)
{
        System.Net.ServicePointManager.CertificatePolicy = new MyPolicy(); 
       // this is CertificatePolicy  subclass
        try
        {
            string url = "https://myirl.com";
            HttpWebRequest req=(HttpWebRequest)WebRequest.Create(url);
            req.KeepAlive = false;
            req.AllowAutoRedirect = true;
            req.ProtocolVersion = HttpVersion.Version10;
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            byte[] postBytes = Encoding.ASCII.GetBytes("username=sample_post_data");
            req.ContentType = "application/x-www-form-urlencoded";
            req.ContentLength = postBytes.Length;
            Stream requestStream = req.GetRequestStream();
            requestStream.Write(postBytes, 0, postBytes.Length);
            requestStream.Close();            

            HttpWebResponse response = (HttpWebResponse)req.GetResponse();
            Stream resStream = response.GetResponseStream();

            var sr = new StreamReader(response.GetResponseStream());
            string responseText = sr.ReadToEnd();
            Response.Write(responseText);
        }
        catch (Exception ee)
        {
        }
    }
}

If you want to convert the response test to xml, then add the xml namespace as using System.Xml; then add the following code below the responseText as
XmlDocument doc = new XmlDocument();
doc.LoadXml(responseText);
Now you can use this xml result according to your requirement.

Thursday, August 20, 2009

Convert String to System.Drawing.Color in ASPX C#

System.Drawing.Color c = System.Drawing.ColorTranslator.FromHtml("#F5F7F8");
String strHtmlColor = System.Drawing.ColorTranslator.ToHtml(c);

Monday, April 13, 2009

JavaScript alert from UpdatePanel in ASPX (C#)

public void ClientMsg(string MsgTxt)
    {
        ScriptManager.RegisterClientScriptBlock(
        this.Page, 
        this.Page.GetType(), 
        "clientScript", 
        "alert('" + MsgTxt + "')", 
        true);
    }