Exemple #1
0
        public static iojson PostWebContent(ref HTTPData httpdata, string url, string jsonStr)
        {
            iojson i = new iojson();

            try
            {
                Debug.WriteLine("PostWebContent.url: " + url);
                Debug.WriteLine("PostWebContent.jsonStr: " + jsonStr);

                // NOTE: this is a temporary remove SSL ceritficate check solution. Please comment out this line in production.
                if (ConfigurationManager.AppSettings["IsFakeSSLCert"] == "yes")
                {
                    ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
                }

                var client = new HttpClient(new HttpClientHandler
                {
                    AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
                    CookieContainer        = httpdata.Cookie,
                });
                client.BaseAddress = new Uri(GetWebSrvURL());
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Add("X-CSRF-Token", httpdata.CSRFtoken);

                //var content = new FormUrlEncodedContent(new[] {
                //    new KeyValuePair<string, string>("", "login")
                //});
                StringContent       content  = new StringContent(jsonStr, Encoding.UTF8, "application/json");
                HttpResponseMessage response = client.PostAsync(url, content).Result;

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

                string resultContent = response.Content.ReadAsStringAsync().Result;

                // set CSRF token
                IEnumerable <string> values;

                if (response.Headers.TryGetValues("X-CSRF-Token", out values))
                {
                    httpdata.CSRFtoken = values.First();
                }

                i.Decode(resultContent);
            }
            catch (Exception e)
            {
                // TODO: need to improve.
                i.AddError("PostWebContent: " + e.Message.ToString());
            }

            return(i);
        }
        public MainWindow()
        {
            InitializeComponent();

            SrvReqBtn.IsEnabled = false;

            httpdata = new Lib.HTTPData()
            {
                Cookie    = new CookieContainer(),
                CSRFtoken = "",
            };
        }
Exemple #3
0
        public static iojson UploadFile(ref HTTPData httpdata, string url, string[] fileNameArr)
        {
            iojson i = new iojson();

            try
            {
                Debug.WriteLine("PostWebContent.url: " + url);

                // NOTE: this is a temporary remove SSL ceritficate check solution. Please comment out this line in production.
                if (ConfigurationManager.AppSettings["IsFakeSSLCert"] == "yes")
                {
                    ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
                }

                var client = new HttpClient(new HttpClientHandler
                {
                    AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
                    CookieContainer        = httpdata.Cookie,
                });
                client.BaseAddress = new Uri(GetWebSrvURL());
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Add("X-CSRF-Token", httpdata.CSRFtoken);

                //var content = new FormUrlEncodedContent(new[] {
                //    new KeyValuePair<string, string>("", "login")
                //});

                MultipartFormDataContent form = new MultipartFormDataContent();

                //form.Add(new StringContent("Jun"), "username");

                for (int c = 0; c < fileNameArr.Length; c++)
                {
                    string fileNameFull = fileNameArr[c];
                    byte[] MyFile       = StreamFile(fileNameFull);
                    string fileName     = Path.GetFileName(fileNameFull);
                    form.Add(new ByteArrayContent(MyFile, 0, MyFile.Count()), "files", fileName);
                }

                HttpResponseMessage response = client.PostAsync(url, form).Result;

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

                client.Dispose();

                string resultContent = response.Content.ReadAsStringAsync().Result;

                // set CSRF token
                IEnumerable <string> values;

                if (response.Headers.TryGetValues("X-CSRF-Token", out values))
                {
                    httpdata.CSRFtoken = values.First();
                }

                i.Decode(resultContent);
            }
            catch (Exception e)
            {
                // TODO: need to improve.
                i.AddError("UploadFile: " + e.Message.ToString());
            }

            return(i);
        }