在c#中发布请求,以便将Api用于c#

在c#中发布请求,以便将Api用于c#,c#,api,curl,post,http-post,C#,Api,Curl,Post,Http Post,我尝试使用vultr api来创建服务器,我已经成功地使用了get方法来获取数据,但我不知道如何在post中创建? 这是用于创建服务器的命令: curl -H 'API-Key: EXAMPLE' https://api.vultr.com/v1/server/app_change --data 'SUBID=596965' --data 'APPID=2' 这是我的代码: using System; using System.Net; using System.IO; namespace

我尝试使用vultr api来创建服务器,我已经成功地使用了get方法来获取数据,但我不知道如何在post中创建? 这是用于创建服务器的命令:

curl -H 'API-Key: EXAMPLE' https://api.vultr.com/v1/server/app_change --data 'SUBID=596965' --data 'APPID=2'
这是我的代码:

using System;
using System.Net;
using System.IO;

namespace Http
{
    class Requete
    {
        [STAThread]
        static void Main(string[] args)
        {
            string Data = "--data 'DCID=24' --data 'VPSPLANID=201' --data 'OSID=152'";
            string Reponse = String.Empty;
            StreamWriter Sw = null; // Pour écrire les données
            StreamReader Sr = null; // Pour lire les données

            try
            {
                HttpWebRequest Req = (HttpWebRequest)WebRequest.Create("https://api.vultr.com/v1/server/create");
                Req.Method = "POST"; // POST ou GET
                Req.ContentType = "application/x-www-form-urlencoded"; // La plupart du temps
                Req.ContentLength = Data.Length; // La longueur des données
                Req.Headers.Add("API-Key: XXXXXXXXXXXXXXXXXXXXXX");


                Sw = new StreamWriter(Req.GetRequestStream());
                Sw.Write(Data); // On écrit les données
                Sw.Close(); // Puis on ferme le flux
                Sw = null;

                Sr = new StreamReader(((HttpWebResponse)Req.GetResponse()).GetResponseStream());
                Reponse = Sr.ReadToEnd(); // On choppe la réponse
                Sr.Close(); // Et on ferme
                Sw = null;
            }
            catch (Exception e) // En cas d'exception
            {
                if (Sw != null) // Si le flux est ouvert, on le ferme
                    Sw.Close();
                if (Sr != null)
                    Sr.Close();

                Console.WriteLine(e.Message + "\r\n\r\nL'application va maintenant se terminer...");

                return;
            }

            Console.WriteLine(Reponse);
        }
    }
}

提前感谢您

数据值不正确,应该是:

 string Data = "DCID=24&VPSPLANID=201&OSID=152";
此外,为了便于阅读,您可以替换:

Sw = new StreamWriter(Req.GetRequestStream());
Sw.Write(Data);
Sw.Close();
Sw = null;


您可以对StreamReader执行相同的操作。如果数据值不正确,则应为:

 string Data = "DCID=24&VPSPLANID=201&OSID=152";
此外,为了便于阅读,您可以替换:

Sw = new StreamWriter(Req.GetRequestStream());
Sw.Write(Data);
Sw.Close();
Sw = null;


您也可以使用StreamReader

Merci beaucoupça marche冻糕Merci beaucoupça marche冻糕