How to run a URL by passing C#parameters

Asked

Viewed 1,462 times

0

Hello,

I’m doing for study purposes, a way to search for and bring vehicle information from the FIPE table straight from the api(http://fipeapi.appspot.com/). What I’m unable to do at first is run the URL by passing parameters. For example:

I need to order the list of vehicle brands according to type, passing the type of vehicle (car, motorcycle or truck). I made a combobox with these options to be chosen by the user and stored in a variable of type 'Object'.

Object tipoSelecionado = cmbTipo.SelectedItem;

Now what I can’t do is at the url: http://fipeapi.appspot.com/api/1/[type]/[action]/[parameters]. json pass the variable in the [type] field 'tipoSelecionado' and store the result in Json in the array 'Marcas'.

public class Marcas
        {
            public string key { get; set; }
            public string name { get; set; }
            public string id { get; set; }
            public string fipe_name { get; set; }
        }

Thanks in advance.

  • I do not understand very well what you want, but if the answer does not help you, explain better that I update the same.

1 answer

0


You can use the Webclient() to do so. This would be an example:

 using (var client = new WebClient())
            {   
                //url de destino. Aqui você pode colocar os parâmetros que desejar
                var url = "http://fipeapi.appspot.com/api/1/carros/veiculos/21.json"; 
                //Download do resultado
                var json = client.DownloadString(url);
                var serializer = new JavaScriptSerializer();
                //Descerialização em uma lista dinâmica. Pode adequar sua classe, se rpeferir
                var model = serializer.Deserialize<dynamic>(json);

            }

In this example, your variable model already have all the elements that will be returned.

In this answer i have an example of how to implement using Asp.NET MVC.

If you are using Winforms, just do it this way:

Object selectedItem = comboBox1.get_SelectedItem();
using (var client = new WebClient())
                {   
                    //url de destino. Aqui você pode colocar os parâmetros que desejar
                    var url = "http://fipeapi.appspot.com/api/1/" + selectedItem.ToString() + "/veiculos/21.json"; 
                    //Download do resultado
                    var json = client.DownloadString(url);
                    var serializer = new JavaScriptSerializer();
                    //Descerialização em uma lista dinâmica. Pode adequar sua classe, se rpeferir
                    var model = serializer.Deserialize<dynamic>(json);
    
                }

This way you will concatenate the values in the url.

  • I understood, exactly this that I wanted to understand, only lacked a detail, this is the link of the form that must be requested right (http://fipeapi.appspot.com/api/1/[type]/[acao]/[parametros].jso), see that this link is between brackets the item type([type]). As I put in this exact location the information that was chosen in the combobox that I created to choose the type of vehicle, ie whether it is car, bike or truck?

  • @Stephenwillians I edited the answer. You haven’t explained how you’re doing it or the language. I made based on Windows Forms, if another, edit your question that improve the answer.

  • Sorry @Randrade, but I am using the C# language in Visual Studio 2013. Can I assign the result in an array to be displayed in a Datagridview or Combobox? Ex.: jsList[ ] = serialize.Deserialize<Dynamic>(json);

  • @Stephenwillians It’s already on a list, just add to your DataGridView. If you want more explanations, open another question, I’ll be happy to help.

  • Show @Randrade, thank you so much. Anything else I’ll open another question.

  • In this case I am using C#(Windows Forms) can I exchange var for string? In the Model variable you declared, which format can I use?

  • @Stephenwillians O var gets the type yourself. But if you want to declare, you can, but not by String. In case you would need to declare an object array, this way: object[] model = serializer.Deserialize<dynamic>(json);

Show 2 more comments

Browser other questions tagged

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