Exemplo n.º 1
0
        private WhoAmIResponse GetWhoAmIRequest()
        {
            HttpCrmClient.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
            HttpCrmClient.DefaultRequestHeaders.Add("OData-Version", "4.0");
            HttpCrmClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            // Use the WhoAmI function
            HttpResponseMessage response = HttpCrmClient.GetAsync("WhoAmI").GetAwaiter().GetResult();

            //Get the response content and parse it.
            string responseBody = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();

            if (response.IsSuccessStatusCode)
            {
                _whoAmI = JSONSerializer <WhoAmIResponse> .DeSerialize(responseBody);

                return(_whoAmI);
            }
            else
            {
                throw new Exception(string.Format(
                                        "The WhoAmI request failed with a status of '{0}' and exception:{1}",
                                        response.ReasonPhrase, responseBody));
            }
        }
Exemplo n.º 2
0
        public static async Task <bool> login(string username, string password, string hwid, Awesomium.Core.JSObject callbackarg)
        {
            var values = new Dictionary <string, string>();

            values.Add("username", username);
            values.Add("password", password);
            values.Add("hwid", hwid);
            var content = new FormUrlEncodedContent(values);

            using (var client = new HttpClient())
            {
                try
                {
                    client.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "text/html,application/xhtml+xml,application/xml");
                    client.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Encoding", "gzip, deflate");
                    client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0");
                    client.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Charset", "ISO-8859-1");
                    var httpResponseMessage = await client.PostAsync("http://handsfreeleveler.com:4446/api/remotelogin", content);

                    if (httpResponseMessage.StatusCode == HttpStatusCode.OK)
                    {
                        string data = await httpResponseMessage.Content.ReadAsStringAsync();

                        var res = JSONSerializer <serviceRes> .DeSerialize(data);

                        if (res.err != null && res.userData == null)
                        {
                            callbackarg?.Invoke("call", callbackarg, false);
                            MessageBox.Show(res.err);
                            return(false);
                        }
                        else
                        {
                            if (res.userData != null)
                            {
                                App.Client = JSONSerializer <User> .DeSerialize(data);

                                callbackarg?.Invoke("call", callbackarg, data);
                                LoginContract loginDetails = new LoginContract();
                                loginDetails.username = username;
                                loginDetails.password = password;
                                Storage.SerializeObject(loginDetails, "loginDetails.xml");
                                return(true);
                            }
                            else
                            {
                                callbackarg?.Invoke("call", callbackarg, false);
                                return(false);
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("Server not responding to your request.");
                        callbackarg?.Invoke("call", callbackarg, false);
                        return(false);
                    }
                }
                catch (Exception ex)
                {
                    callbackarg?.Invoke("call", callbackarg, false);
                    return(false);
                }
            }
        }
Exemplo n.º 3
0
        public async Task <ResourcesZnode> GetResourcesAsync(Watcher childWatcher, Watcher dataWatcher)
        {
            var actionToPerform = "get the list of resources";

            while (true)
            {
                await BlockUntilConnected(actionToPerform);

                try
                {
                    DataResult dataResult = null;
                    if (dataWatcher != null)
                    {
                        dataResult = await this.zookeeper.getDataAsync(this.resourcesPath, dataWatcher);
                    }
                    else
                    {
                        dataResult = await this.zookeeper.getDataAsync(this.resourcesPath);
                    }

                    ChildrenResult childrenResult = null;
                    if (childWatcher != null)
                    {
                        childrenResult = await this.zookeeper.getChildrenAsync(this.resourcesPath, childWatcher);
                    }
                    else
                    {
                        childrenResult = await this.zookeeper.getChildrenAsync(this.resourcesPath);
                    }

                    var resourcesZnodeData = JSONSerializer <ResourcesZnodeData> .DeSerialize(
                        System.Text.Encoding.UTF8.GetString(dataResult.Data));

                    if (resourcesZnodeData == null)
                    {
                        resourcesZnodeData = new ResourcesZnodeData();
                    }

                    return(new ResourcesZnode()
                    {
                        ResourceAssignments = resourcesZnodeData,
                        Resources = childrenResult.Children,
                        Version = dataResult.Stat.getVersion()
                    });
                }
                catch (KeeperException.NoNodeException e)
                {
                    throw new ZkInvalidOperationException(
                              $"Could not {actionToPerform} as the resources node does not exist.", e);
                }
                catch (KeeperException.ConnectionLossException)
                {
                    // do nothing, the next iteration will try again
                }
                catch (KeeperException.SessionExpiredException e)
                {
                    throw new ZkSessionExpiredException($"Could not {actionToPerform} as the session has expired: ", e);
                }
                catch (Exception e)
                {
                    throw new ZkInvalidOperationException($"Could not {actionToPerform} due to an unexpected error", e);
                }
            }
        }
Exemplo n.º 4
0
        public override OcrResult Load(OcrImage image, string language, string apiLanguage)
        {
            OcrRequests request = new OcrRequests()
            {
                requests = new OcrRequest[1]
                {
                    new OcrRequest()
                    {
                        image = new OcrRequestImage()
                        {
                            content = Convert.ToBase64String(image.Data)
                        },
                        features = new OcrRequestFeature[1]
                        {
                            new OcrRequestFeature()
                            {
                                type       = "TEXT_DETECTION",
                                maxResults = 1
                            }
                        }
                    }
                }
            };

            string requestJson = JSONSerializer <OcrRequests> .Serialize(request);

            using (GZipWebClient webClient = new GZipWebClient())
            {
                string key = Program.Settings.KeyInfo.Key;

                if (string.IsNullOrEmpty(key))
                {
                    key = GetDefaultKey();
                }

                string url = "https://vision.googleapis.com/v1/images:annotate?key=" + key;

                webClient.Proxy    = null;
                webClient.Encoding = Encoding.UTF8;
                webClient.Headers[HttpRequestHeader.ContentType]    = "application/json";
                webClient.Headers[HttpRequestHeader.Accept]         = "application/json";
                webClient.Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate";
                string json = webClient.UploadString(url, requestJson);

                string       result    = string.Empty;
                OcrResponses responses = JSONSerializer <OcrResponses> .DeSerialize(json);

                foreach (OcrResponse response in responses.responses)
                {
                    foreach (OcrTextAnnotation annotation in response.textAnnotations)
                    {
                        if (!string.IsNullOrEmpty(annotation.locale))
                        {
                            result += annotation.description;
                        }
                    }
                }
                result = result.TrimEnd('\r', '\n');
                return(new OcrResult(result));
            }
        }