Exemple #1
0
        public string Post2Server(string apiName, string sMethod, string sData)
        {
            string apiUrl     = FrmConfig.GetConfig(FrmConfig.KEY_URL);
            string _urlAPI    = apiUrl + apiName;
            string _strReturn = "";
            string uid        = FrmConfig.GetConfig(FrmConfig.KEY_LOGIN_NAME);
            string pwd        = FrmConfig.GetConfig(FrmConfig.KEY_PASSWORD);

            try
            {
                ServicePointManager.ServerCertificateValidationCallback -= new System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate);
            }
            catch { }
            if (apiUrl.Contains("https:"))
            {
                ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate);
            }

            //using (WebClient wc = new WebClient())
            //{

            //    wc.Headers[HttpRequestHeader.UserAgent] = "538c8b5f-2fb8-42be-a4ab-63fbee68e6af";
            //    wc.Headers[HttpRequestHeader.Accept] = "application/json"; //;
            //    wc.Headers[HttpRequestHeader.ContentType] = "application/json";//;charset=UTF-8

            //    string authen_token = GenerateToken(uid, pwd, sMethod);
            //    // wc.Headers[HttpRequestHeader.Authorization] = authen_token;
            //    wc.Headers.Add("Authentication", authen_token);
            //    wc.Encoding = UTF8Encoding.UTF8;
            //    try
            //    {
            //        lock (_KeyToPost)
            //        {
            //            if (sMethod == SMethod.GET)
            //                _strReturn = System.Text.Encoding.UTF8.GetString(wc.DownloadData(_urlAPI + "?" + sData));
            //            else
            //                _strReturn = wc.UploadString(_urlAPI, sMethod, sData);
            //        }
            //    }
            //    catch(WebException we)
            //    {
            //        if (we.Response is HttpWebResponse)
            //        {
            //            HttpWebResponse r = we.Response as HttpWebResponse;
            //            using (StreamReader sr = new StreamReader(r.GetResponseStream()))
            //            {
            //                _strReturn = sr.ReadToEnd();
            //            }
            //        }
            //    }
            //    catch(Exception ex)
            //    {
            //        throw;
            //    }

            //}
            //string authen_token = EasyRequest.GenAuthentication(uid, pwd, sMethod);// GenerateToken(uid, pwd, sMethod);
            ////HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_urlAPI);
            ////request.UserAgent = "EasyInvoice-Client/1.0.0.0";
            ////request.Accept = "application/json, text/json";
            ////request.ContentType = "application/json";
            ////request.Method = sMethod;
            ////request.Headers.Add("Authentication", authen_token);
            ////request.ServicePoint.Expect100Continue = false;
            ////request.KeepAlive = true;
            ////request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip | DecompressionMethods.None;

            ////using (Stream sw = request.GetRequestStream())
            ////{
            ////    byte[] d=Encoding.UTF8.GetBytes(sData);
            ////    sw.Write(d,0,d.Length);
            ////    sw.Flush();
            ////}
            ////HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            ////using (StreamReader sr = new StreamReader(response.GetResponseStream()))
            ////{
            ////    _strReturn = sr.ReadToEnd();
            ////}

            EasyClient   CLIENT  = new EasyClient(apiUrl, uid, pwd);
            EasyResponse respone = CLIENT.PostJsonObject(apiName, sData);

            _strReturn = respone.Content;


            return(_strReturn);
        }