U
U
up72020-04-17 13:09:12
C++ / C#
up7, 2020-04-17 13:09:12

How to make a request correctly?

There is a code in PHP, I translate it into C#:

$fields = array(
            'Page' => $page,
      'Sides' => array(
            ),
        );

$fields_string = http_build_query($fields);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);


That is, I send a post request:

var values = new List<KeyValuePair<string, string>>
            {
                new KeyValuePair<string, string>("Page", Page)
            };

//как правильно тут добавить массив?

var content = new FormUrlEncodedContent(values);
var response = await client.PostAsync(ur, content);


In other words, how to shove an array into a string, so that FormUrlEncodedConten can chew it up later?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
twobomb, 2020-04-17
@up7

Try

var values = new List<KeyValuePair<string, string>>
            {
                new KeyValuePair<string, string>("Page", Page),
                new KeyValuePair<string, string>("Sides[0]", side1),
                new KeyValuePair<string, string>("Sides[1]", side2),
                   ....
            };

Or
var values = new List<KeyValuePair<string, string>>
            {
                new KeyValuePair<string, string>("Page", Page)
            };
for(var i = 0; i < sides.Length;i++)
  values.Add( new KeyValuePair<string, string>("Sides["+i+"]", sides[i]));

PS In extreme cases, you can write your own http_build_query method, which will accept some kind of Dictinary <string,object>, and intelligently form this array. Although there may already be some similar method, you need to read the docs

U
up7, 2020-04-17
@up7

I solved it with a simple manual conversion, if anyone is interested:
string json = "{'Page':1,'Count':25,'Sides':['"+ Sides + "']}";

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question