Wednesday, October 26, 2016

Get JSON data through ajax

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Windows.Input;

namespace AutoCompleteSample
{
    class StdLib
    {
        public static bool AcceptAllCertifications(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certification, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
        {
            return true;
        }

        public static async Task<string> AjaxV1(string uri)
        {
            try
            {
                // TODO: this is a temporary remove SSL ceritficate check solution. Please comment out this line in production.
                ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);

                var httpClient = new HttpClient(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate });
                httpClient .DefaultRequestHeaders.Accept.Clear();
                httpClient .DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                var response = await httpClient.GetAsync(uri);

                // will throw an exception if not successful
                response.EnsureSuccessStatusCode();

                //return await Task.Run(() => JsonObject.Parse(content));
                //return await Task.Run(() => content);
                return await response.Content.ReadAsStringAsync();
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                return "{Status:false}";
            }
        }

        public static async Task<string> AjaxV2(string uri)
        {
            try
            {
                // TODO: this is a temporary remove SSL ceritficate check solution. Please comment out this line in production.
                ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);

                var httpClient = new HttpClient();
                return await httpClient.GetStringAsync(uri);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                return "{Status:false}";
            }
        }

        public static char ConvertKeyToChar(Key key)
        {
            return Convert.ToChar(KeyInterop.VirtualKeyFromKey(key));
        }

        public static string GetWebSrvURL()
        {
            string url = "";
            url += ConfigurationManager.AppSettings["WebSrvProtocol"] + "://" + ConfigurationManager.AppSettings["WebSrvDomain"] + ":" + ConfigurationManager.AppSettings["WebSrvPort"];
            return url;
        }
    }
}

No comments: