示例#1
0
    static async Task MakeRequest()
    {
        // Create a new client
        HttpClient client = new HttpClient();

        // Add appropriate request headers (API subscription key)
        client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", lib.v["sub-key"]);

        // Request query string.
        var queryString = HttpUtility.ParseQueryString(string.Empty);

        queryString["mode"] = "spell";
        var uri = lib.v["url"] + queryString;

        // Prepare the response
        HttpResponseMessage response;

        // Request body
        var paramString = HttpUtility.ParseQueryString(string.Empty);

        paramString["text"] = input; // replace all spaces with +?

        byte[] payload = Encoding.UTF8.GetBytes(paramString.ToString());
        Debug.WriteLine(paramString.ToString());
        using (var content = new ByteArrayContent(payload))
        {
            content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
            response = await client.PostAsync(uri, content);

            // Placeholder will store the value to update the OutputLabel.
            var responseBody   = response.Content;
            var serializedJson = await responseBody.ReadAsStringAsync();

            JObject json = (JObject)JsonConvert.DeserializeObject(serializedJson);

            JsonParser parser = new JsonParser(json, input);

            if (parser.BadResponse())
            {
                placeholder = lib.Error(input.Length);
                Debug.WriteLine(json.ToString());
            }
            else
            {
                placeholder = parser.GetResults();
            }
        }
    }