c# Consume an API using POST and header application/x-www-form-urlencoded by passing a JSON

Asked

Viewed 4,288 times

1

can give me a light?

I need to consume an API that requires your header to have one content-type: application/x-www-form-urlencoded. This api takes json parameters and returns json. Using jquery I can but need to create a console app to run via windows task scheduler, so I need to do it using Asp.net web api c# or something like that

$.ajax({
        contentType: 'application/x-www-form-urlencoded',
        crossDomain: true,
        type: "POST",
        url: 'url',
        data: JSON.stringify(params),
        success: function (data) {
            do it
        },
        error: function (data) {
            do it
        },
        dataType: 'json',
        async: false
    });

1 answer

0


using (var httpClient = new HttpClient())
        {
            var url = @"https://api-il.traffilog.com/appengine_3/5E1DCD81-5138-4A35-B271-E33D71FFFFD9/1/json";

            var jsonString = @"{
'action': {
    'name': 'user_login',
    'parameters': {
                'login_name': 'user',
        'password': '123'
    }
        }
    }";

            var content = new StringContent(jsonString, Encoding.UTF8, "application/json");
            content.Headers.Clear();
            content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

            HttpResponseMessage response = httpClient.PostAsync(url, content).Result;

            if (response.IsSuccessStatusCode)
            {
                Console.WriteLine("ok");
            }
            else
            {
                Console.WriteLine("erro");
            }
        }

        Console.ReadKey();

    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.