简体   繁体   English

与CURL类似的C#API POST

[英]C# API POST similar to CURL

I'm trying to use the vultr.com API that utilizes CURL. 我正在尝试使用利用CURL的vultr.com API。 I have successfully completed a POST call to their API with several different calls, but one in particular will not work. 我已经用几个不同的调用成功完成了对它们的API的POST调用,但是特别是其中一个将不起作用。 Here is their documentation on the API https://www.vultr.com/api/ . 这是有关API https://www.vultr.com/api/的文档。

I am able to get their server/create and firewall/rule_create to work properly with pretty much the same exact code, but it will not work for their server/reboot command. 我能够使他们的服务器/创建和防火墙/ rule_create在几乎相同的完全相同的代码下正常工作,但不适用于他们的服务器/重启命令。 Now here is code for a post to their API that I have successfully gotten to work. 现在,这里是我成功开始工作的API帖子代码。

public static void AddIpToFirewall(string ip,string fireWallGroupId)
{
    string Data = "FIREWALLGROUPID=" + fireWallGroupId + "&direction=in&ip_type=v4&protocol=tcp&subnet=" + ip + "&subnet_size=32&port=80";
    string Reponse = String.Empty;
    StreamWriter Sw = null;
    StreamReader Sr = null;
    try
    {
        HttpWebRequest Req = (HttpWebRequest)WebRequest.Create("https://api.vultr.com/v1/firewall/rule_create");
        Req.Method = "POST";
        Req.ContentType = "application/x-www-form-urlencoded";
        Req.ContentLength = Data.Length;
        Req.Headers.Add(ApiKey);
        using (var sw = new StreamWriter(Req.GetRequestStream()))
        {
            sw.Write(Data);
        }
        Sr = new
        StreamReader(((HttpWebResponse)Req.GetResponse()).GetResponseStream());
        Reponse = Sr.ReadToEnd();
        Sr.Close();
    }
    catch (Exception ex)
    {
        if (Sw != null)
            Sw.Close();
        if (Sr != null)
            Sr.Close();
        Console.WriteLine(ex.Message + "\r\n\r\n'error with vultr...");
    }
}

Now here is the code that will not work. 现在,这里的代码将无法正常工作。 It's pretty much identical to the firewall POST command. 它与防火墙POST命令几乎相同。

public static void RebootCommand(string subId)
{
    string Data = "SUBID=" + subId;
    string Reponse = String.Empty;
    StreamWriter Sw = null;
    StreamReader Sr = null;
    try
    {
        HttpWebRequest Req = (HttpWebRequest)WebRequest.Create("https://api.vultr.com/v1/server/reboot");
        Req.Method = "POST";
        Req.ContentType = "application/x-www-form-urlencoded";
        Req.ContentLength = Data.Length;
        Req.Headers.Add(ApiKey);
        using (var sw = new StreamWriter(Req.GetRequestStream()))
        {
            sw.Write(Data);
        }
        Sr = new
        StreamReader(((HttpWebResponse)Req.GetResponse()).GetResponseStream());
        Reponse = Sr.ReadToEnd();
        Sr.Close();
    }
    catch (Exception ex)
    {
        if (Sw != null)
            Sw.Close();
        if (Sr != null)
            Sr.Close();
        Console.WriteLine(ex.Message + " error with vultr...");
    }
}

It does not receive an error, but fails to reboot the VM. 它没有收到错误,但是无法重新引导VM。 I've contacted their support and they say that their reboot command is working without any issues. 我已经联系了他们的支持人员,他们说他们的重新启动命令可以正常工作。 Is there anyone out there that has had this issue before? 有没有人以前有过这个问题? Thanks. 谢谢。

I figured it out. 我想到了。 With all of the other POST requests that I have done from their API the line Req.ContentLength = Data.Length was required. 对于我从其API执行的所有其他POST请求,需要Req.ContentLength = Data.Length行。 But for whatever reason that line had to be removed to get the reboot command to work. 但是无论出于何种原因,都必须删除该行才能使reboot命令起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM