예제 #1
0
    public void ObtenLista2()
    {
        info          = "OK"; // optimismo
        todoOK        = true;
        RootLanguages = new RootObjectLanguages();
        var Languages = new List <Language>();
        var Language  = new Language();

        Language.name           = "English(UK)"; Language.code = "English"; Languages.Add(Language);
        Language                = new Language();
        Language.name           = "Spanish (Spain)"; Language.code = "Spanish"; Languages.Add(Language);
        RootLanguages.languages = Languages;


        return;
    }
예제 #2
0
    public void ObtenLista()
    {
        info          = "OK";                      // optimismo
        todoOK        = true;
        RootLanguages = new RootObjectLanguages(); // objeto
        HttpClient client = new HttpClient();

        client.BaseAddress = new Uri(host); // "https://api.strakertranslations.com:443");
        // Add an Accept header for JSON format.
        client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));
        HttpResponseMessage response = client.GetAsync("/v3/languages").Result;  // Blocking call! Program will wait here until a response is received or a timeout occurs.
                                                                                 // la forma síncrona (que hay que encapular en una "static async Task<xxx> Nombre(); es
                                                                                 // HttpResponseMessage response = await client.GetAsync(path);
                                                                                 //use JavaScriptSerializer from System.Web.Script.Serialization
        JavaScriptSerializer JSserializer = new JavaScriptSerializer();

        if (response.IsSuccessStatusCode)
        {
            // Parse the response body.
            // dentro de tarea síncrona string data = await response.Content.ReadAsStringAsync();
            string data = response.Content.ReadAsStringAsync().Result;            // sin asycn y con Result;
            RootLanguages = JSserializer.Deserialize <RootObjectLanguages>(data); // respuesta convertida en clase.
            // info= CF.FormatOutput2HTML(data); // Raw JSON Response
        }
        else
        {
            todoOK = false;
            info   = string.Format("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
        }

        //Make any other calls using HttpClient here.



        //Dispose once all HttpClient calls are complete. This is not necessary if the containing object will be disposed of; for example in this case the HttpClient instance will be disposed automatically when the application terminates so the following call is superfluous.
        client.Dispose();


        return;
    }