How to send / receive cURL/API requests in C#

后端 未结 1 1447
生来不讨喜
生来不讨喜 2021-01-22 14:59

i have problems with my c# programm to send or receive cURL requests to a online telephone system, i hope to get some help there :)

I want to send commands like this:

1条回答
  •  臣服心动
    2021-01-22 15:54

    I prefer using WebClient

    WebClient wc = new WebClient();
    var buf = wc.UploadValues("https://api.placetel.de/api/test", 
                               new NameValueCollection() { { "api_key", "XXXX" } });
    var xml = Encoding.UTF8.GetString(buf);
    

    or HttpClient

    HttpClient client = new HttpClient();
    var content = new FormUrlEncodedContent(new Dictionary() { 
            { "api_key", "XXXX" } 
    });
    var resp =  await client.PostAsync("https://api.placetel.de/api/test",content);
    var xml = await resp.Content.ReadAsStringAsync();
    

    which have methods easier to use.

    BTW, -d (or --data) is a parameter of curl, it is not sent to server

    PS: You may also want to read something about ContentType http://www.freeformatter.com/mime-types-list.html

    0 讨论(0)
提交回复
热议问题