/// <summary>
        /// Send data to Filt, and return result.
        /// </summary>
        public FiltResponse Send(FiltRequest Request, bool Verify = true)
        {
            // init handler
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            if (!Verify)
            {
                ServicePointManager.ServerCertificateValidationCallback =
                    new RemoteCertificateValidationCallback(delegate { return(true); });
            }

            // send to filt server
            WebClient web = new WebClient();

            web.Headers[HttpRequestHeader.ContentType] = "application/json;charset=UTF-8";
            web.Headers[HttpRequestHeader.Accept]      = "application/json";
            var hoge = Request.ToJsonString();

            Debug.Log(hoge);

            // deserialize to object
            string string_response = web.UploadString(new Uri(Url), hoge);

            web.Dispose();

            return(FiltResponse.FromString(string_response));
        }
Exemple #2
0
        /// <summary>
        /// Send data to Filt, and return result.
        /// </summary>
        public async Task <FiltResponse> Send(FiltRequest filt_request, bool verify = true)
        {
            // init handler that called checking certification
            var handler = new HttpClientHandler()
            {
                ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) =>
                {
                    switch (sslPolicyErrors)
                    {
                    case SslPolicyErrors.None:
                    case SslPolicyErrors.RemoteCertificateNameMismatch:
                        return(true);

                    default:
                        return(!verify);
                    }
                }
            };

            // init request data
            Dictionary <string, string> dict_request;

            if (filt_request.Option == null)
            {
                dict_request = new Dictionary <string, string>();
            }
            else
            {
                dict_request = filt_request.Option;
            }

            // convert target to basa64
            string base64_target = Convert.ToBase64String(filt_request.Target);

            dict_request["target"] = base64_target;

            // convert dictionary to json string
            string json_request = JsonConvert.SerializeObject(dict_request);

            // send request
            var client  = new HttpClient(handler);
            var content = new StringContent(json_request, Encoding.UTF8, "application/json");

            HttpResponseMessage response;

            try {
                response = await client.PostAsync(Url, content);
            } catch {
                throw new Exception("Faild to communicate with Filt");
            }

            try {
                string body = await response.Content.ReadAsStringAsync();

                System.Console.WriteLine(body);
                FiltJsonResponse json_response = JsonConvert.DeserializeObject <FiltJsonResponse>(body);

                return(json_response.Deserialize());
            } catch {
                throw new Exception("Faild to desirialize to FiltResponse");
            }
        }